自动映射器查找未映射的属性

时间:2018-07-27 12:34:28

标签: c# .net entity-framework model-view-controller automapper

使用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的每个成员。不知道我在想什么。可能很简单

1 个答案:

答案 0 :(得分:1)

由于您的来源是List,因此您还需要将其映射到List

var rslt = _mapper.Map<List<DestObj>>(items);