![](/img/trans.png)
[英]AutoMapper behaviour with readonly properties in source not being mapped to destination
[英]Automapper Finding Not Mapped properties
使用Automapper进行项目,只需将2个对象相互映射,就不用花哨了。 我必须配置错误,因为AutoMapper一直说有未映射的属性。
这是AutoMapper配置。
var mapperConfig = new MapperConfiguration(cfg => {cfg.CreateMap<SrcObj, DestObj>()
.ForMember(dest => dest.Level, opt => opt.MapFrom(src => src.lvl));}
mapperConfig.AssertConfigurationIsValid();
SrcObj
public class SrcObj
{
public int Id { get; set; }
public int ParentNode { get; set; }
public string Controller { get; set; }
public string Action { get; set; }
public string DisplayName { get; set; }
public string Description { get; set; }
public bool? IsActive { get; set; }
public string AreaName { get; set; }
public int? DisplayOrder { get; set; }
public Int64 Type{ get; set; }
public int lvl { get; set; }
}
DestObj
public class DestObj
{
public int Id { get; set; }
public int ParentNode { get; set; }
public string Controller { get; set; }
public string Action { get; set; }
public string DisplayName { get; set; }
public string Description { get; set; }
public bool? IsActive { get; set; }
public string AreaName { get; set; }
public int? DisplayOrder { get; set; }
public Int64 Type{ get; set; }
public int Level { get; set; }
}
并执行:
var items = await _context.Database.SqlQuery<SrcObj>($"EXEC spGenerateMenu {app1}").ToListAsync();
var rslt = _mapper.Map<DestObj>(items);
和错误:
{“ \\ n已找到未映射的成员。请查看下面的类型和成员。\\ n添加自定义映射表达方式...}
该错误实际上列出了DestObj的每个成员。 不知道我在想什么。 可能很简单
因为您的来源是一个List
,所以您还需要将其映射到一个List
:
var rslt = _mapper.Map<List<DestObj>>(items);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.