簡體   English   中英

自動映射器查找未映射的屬性

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM