在基于接口为类目标和基于另一个类的源类创建映射时,自动映射器将省略目标类上的新属性,这些属性将使用cutom配置进行映射(使用ForMember和MapFrom)。
源类和源基类:
public class SourceDictionary: SourceBaseDictionary
{
public SourceDictionary(int id, string code, string name);
}
public class SourceBaseDictionary: ISourceDictionary
{
public SourceBaseDictionary();
public int Id { get; protected internal set; }
public string Code { get; protected internal set; }
public string Name { get; protected internal set; }
}
目标类别:
public class DestinationDictionary:IDestinationDictionary
{
public int Id { get; set ; }
public int Value { get; set; }
public string Code { get; set; }
string Label { get; set; }
string Name { get; set; }
}
我对Automapper的配置:
public class AutoMapperDictionary : Profile
{
public AutoMapperDictionary()
{
CreateMap<SourceDictionary, DestinationDictionary>()
.ForMember(vm => vm.Value, map => map.MapFrom(m => m.Id))
.ForMember(vm => vm.Label, map => map.MapFrom(m => m.Name));
}
}
这就是我现在得到的原因:
{
"id": 1,
"value": 0,
"code": "codeTest",
"label": null,
"name": "nameTest"
}
为什么属性值和标签的值与id和name不同?
编辑(带有更多信息)
在启动services.AddMvc()之前,我得到:
services.AddAutoMapper();
Mapper.Initialize(x =>
{
x.AddProfile(new AutoMapperDictionary());
x.ValidateInlineMaps = false;
});
当我使用地图功能时:
var queryResult = await conn.QueryAsync<SourceDictionary>(query);
var newDestinationDictionaryList = queryResult.ToList().Select(req => Mapper.Map<DestinationDictionary>(req))
当我评论“ x.ValidateInlineMaps = false”行时,出现了异常:
AutoMapperConfigurationException ...
找到了未映射的成员。...
未映射的属性:
值
标签