自动映射意外字段c#
我试图将一个简单模型映射到一个实体,但得到了一个未映射项的列表,这是我不希望看到的,它在AutomapperCfg的验证行失败: SaveImportRunDetailsModel->ImportRunDetailEntity(目标成员) 列表)FCSD.Models.saveImportRundeailsModel-> LLBLGEN.EntityClass.ImportRunDetailEntity(目标成员列表) 未映射属性:自动映射意外字段c#,c#,automapper,llblgen,C#,Automapper,Llblgen,我试图将一个简单模型映射到一个实体,但得到了一个未映射项的列表,这是我不希望看到的,它在AutomapperCfg的验证行失败: SaveImportRunDetailsModel->ImportRunDetailEntity(目标成员) 列表)FCSD.Models.saveImportRundeailsModel-> LLBLGEN.EntityClass.ImportRunDetailEntity(目标成员列表) 未映射属性: Id ConcurrencyPredicateFactoryT
Id
ConcurrencyPredicateFactoryToUse
AuthorizerToUse
AuditorToUse
Validator
ActiveContext
IsNew
Fields
IsDirty
这些看起来像是系统生成的项目,有没有办法消除它们
AutomapperCfg.cs是
using AutoMapper;
using FCSD.Models;
using LLBLGEN.EntityClasses;
namespace FCSD.Automapper
{
public class AutomapperCfg : IAutomapperCfg
{
public void Configure()
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<CategoryEntity, Category>(MemberList.Destination);
cfg.CreateMap<EnglishPartInfoEntity, EnglishPartModel>(MemberList.Destination);
cfg.CreateMap<ImageEntity, Image>(MemberList.Destination);
cfg.CreateMap<ImportRunDetailEntity, ImportRunDetailModel>(MemberList.Destination);
cfg.CreateMap<ModelExportBaseEntity, Model>(MemberList.Destination).ReverseMap();
cfg.CreateMap<PartEntity, Part>(MemberList.Destination);
cfg.CreateMap<SaveImportRunDetailsModel, ImportRunDetailEntity>(MemberList.Destination);
});
Mapper.AssertConfigurationIsValid();
}
}
}
最后,ImportRunDetailEntity有点长(超过400行),由LLBLGen Pro自动生成。发生了什么事 如果目标类型包含任何无法与源属性匹配的属性,并且没有明确告知如何填充该属性,则AutoMapper将引发异常 如何修复它 如果不希望AutoMapper填充属性,则需要在
CreateMap()
的返回中使用此扩展方法,以便忽略每个字段:
.ForMember(dest => dest.Id, opt => opt.Ignore())
.ForMember(dest => dest.ConcurrencyPredicateFactoryToUse, opt => opt.Ignore())
.ForMember(dest => dest.AuthorizerToUse, opt => opt.Ignore());
等等
但那太糟糕了…
显然,这是一个拖拽,把“Autoto”直接从AutoMapper中取出,所以你可能想考虑这样的事情——它会自动忽略所有在源对象上不存在的目标成员。p>
还有一件事
您可能希望编写一个单元测试,用所有映射配置一个
Mapper
实例,然后调用Mapper.assertconfigurationsvalid()
在测试时而不是在运行时发现任何问题,默认情况下,AutoMapper在您第一次尝试使用映射之前不会费心验证映射 我从解决方案中删除了AutoMapper,因为它试图在LLBLGen基类中进行处理。为了克服这一点,忽略规则需要我自己编写地图。你尝试过忽略其余的方法吗?汤姆,是的,我尝试过忽略,并在需要时制作单独的反向地图。
.ForMember(dest => dest.Id, opt => opt.Ignore())
.ForMember(dest => dest.ConcurrencyPredicateFactoryToUse, opt => opt.Ignore())
.ForMember(dest => dest.AuthorizerToUse, opt => opt.Ignore());