Tag: #automapper

AutoMapper ForMember忽略不起作用

在MVC应用程序中执行相同实体类型的副本,但希望忽略复制主键(对现有实体进行更新)。 但是,在下面的地图中将Id列设置为忽略不起作用,并且正在覆盖Id。 cfg.CreateMap() .ForMember(dest => dest.Id, option => option.Ignore()) .ForMember(dest => dest.CreatedById, option => option.Ignore()) .ForMember(dest => dest.CreatedOn, option => option.Ignore()) ; 执行地图: existingStratusVendorContact = Mapper.Map(vendorContact); 看到其他答案 ,但看来我已经这样做了。 更新: Fyi,我在Global.asax中创建我的地图,如下所示: Mapper.Initialize(cfg => { cfg.CreateMap() .ForMember(dest => dest.Id, option => option.Ignore()) .ForMember(dest => dest.CreatedById, option => option.Ignore()) .ForMember(dest => dest.CreatedOn, option => option.Ignore()) ; });

避免构造函数映射字段

我正在使用带有.NET Core 2.0的AutoMapper 6.2.2及其默认的dependency injection机制来映射模型和DTO。 我在AutoMapper配置中需要DI,因为我必须执行需要一些注入组件的AfterMap 。 问题是,对于某些具有参数匹配某些源成员的构造函数的模型,当我为AutoMapper启用DI时(添加services.AddAutoMapper() ),这些构造函数默认调用并输入数据,然后用EF中断我的操作。 public class UserDTO { public string Name { get; set; } public string Email { get; set; } public ICollection Roles { get; set; } } public class User { public string Name { get; set; } public string Email { get; set; } public ICollection […]

更新了AutoMapper从3到4打破了inheritance映射

我将AutoMapper从3.3.1更新到4.0.4,这打破了以下映射与此消息 无法将’Foo’类型的对象强制转换为’BarDTO’类型 类 public class FooDTO { // omitted } // derived DTO public class BarDTO : FooDTO { public string Extra { get; set; } } 映射配置 Mapper.CreateMap().ReverseMap(); Mapper.CreateMap(); 制图 Map(foo); // throws cast exception 我也尝试过使用.Include()方法,但没有什么区别。 Mapper.CreateMap() .Include() .ReverseMap(); Mapper.CreateMap(); 我做错了什么,还是一个错误?

是否可以将IQueryable 映射到IQueryable ?

这是Automapper专业人士的问题。 我已经尝试了几天的查询映射 – 并且没有运气。 似乎Automapper并不打算以我想要的方式使用它。 但也许我错了。 所以这是问题…… 我有这样的课程: CatDto(名称,年龄,玩具(ToyDto对象的集合)) ToyDto(CatName,ToyName,Cat(父CatDto对象)) Cat(来自Entity Framework,具有与CatDto类似的属性) 玩具(来自entity framework,具有与ToyDto类似的属性) 我想在我的数据访问层中编写一个通用的读取函数,如下所示: IEnumerable Read(IQueryable query) { // here “query” is converted // to Entity Framework query by means of AutoMapper, // EF query gets executed, // I convert EF entities (Cat) back to CatDto – this is not essential // result is […]

validationASP.Net MVC中视图模型中的嵌套模型

我有一个公司模型的应用程序。 Company模型具有Address模型的导航属性(一对一关系): Company.cs public class Company { public int CompanyID { get; set; } public string Name { get; set; } // Snip… public virtual Address Address { get; set; } } 我创建了一个视图模型来处理编辑,细节和创建操作: CompanyViewModel.cs public class CompanyViewModel { public int CompanyID { get; set; } [Required] [StringLength(75, ErrorMessage = “Company Name cannot exceed 75 characters”)] […]

AutoMapper最佳实践 – 我是否应该向DAO询问有关实现从DTO到域对象的映射的信息?

/// /// Initialize the AutoMapper mappings for the solution. /// http://automapper.codeplex.com/ /// public static void CreateAutoMapperMaps() { IDaoFactory daoFactory = DependencyResolver.Current.GetService(); Mapper.CreateMap() .ReverseMap(); IPlaylistDao playlistDao = daoFactory.GetPlaylistDao(); IUserDao userDao = daoFactory.GetUserDao(); Mapper.CreateMap(); Mapper.CreateMap() .ForMember(playlist => playlist.User, opt => opt.MapFrom(playlistDto => userDao.Get(playlistDto.UserId))); Mapper.CreateMap(); Mapper.CreateMap() .ForMember(playlistItem => playlistItem.Playlist, opt => opt.MapFrom(playlistItemDto => playlistDao.Get(playlistItemDto.PlaylistId))); Mapper.CreateMap().ReverseMap(); Mapper.CreateMap().ReverseMap(); Mapper.CreateMap().ReverseMap(); Mapper.AssertConfigurationIsValid(); […]

Automapper Nuget Package失败

我试图安装http://automapper.org/,但这导致了一个错误。 Install-Package : ‘AutoMapper’ already has a dependency defined for ‘Microsoft.CSharp’. At line:1 char:1 + Install-Package AutoMapper + ~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Install-Package], InvalidOperationException + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.InstallPackageCommand 有任何想法吗?

启动时没有加载Automapper配置文件?

我正在使用: AutoMapper 6.1.1 AutoMapper.Extensions.Microsoft.DependencyInjection 3.0.1 似乎我的配置文件没有被加载,每次我调用mapper.map我得到AutoMapper.AutoMapperMappingException:’缺少类型映射配置或不支持的映射。 这是我的Startup.cs类的ConfigureServices方法 // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddMvc(); //register automapper services.AddAutoMapper(); . . } 在另一个名为xxxMappings的项目中,我有我的映射配置文件。 示例类 public class StatusMappingProfile : Profile { public StatusMappingProfile() { CreateMap() .ForMember(t => t.Id, s => s.MapFrom(d => […]

使用密钥将列表映射到Automapper中的现有列表

Automapper可以轻松处理将一个对象类型列表映射到另一个不同对象类型的列表,但是是否可以使用ID作为键将其映射到现有列表?

AutoMapper – 使用相同的源和目标对象类型映射

我正在使用Automapper来获取两个相同类型的对象,并映射已更改的任何新值。 我尝试使用下面的代码,但它不断抛出错误,我甚至不确定这是否可以通过Automapper实现。 例如: Mapper.CreateMap(); UserDetails userDetails = Mapper.Map(userDetailsCurrent, userDetailsNew); 基本上,我需要将来自新对象“userDetailsNew”的任何新值复制到现有对象“userDetailsCurrent” – 即使它们属于同一类型。 这样我就可以用新值“更新”现有对象。 我这样做的原因是因为我不确定将传递哪些用户详细信息 – 我需要在它们到达时映射它们。 我通常使用Automapper来映射具有相似属性的不同对象 – 但我认为我可以利用Automapper的强大function以这种方式实现相同的function。 甚至可能有更好的解决方案 – 任何帮助将不胜感激!