AutoMapper 5.1 - 地图模型和扩展模型

我是 AutoMapper 的新手,我使用的是 5.1.1 版本。我尝试将模型列表与扩展模型列表进行映射。我得到空对象作为回报。


我的源模型


public class spt_detail

{

    public string Name { get; set; }

    public string Age { get; set; }

}

我的目的地模型


public class spt_detail_extended : spt_detail

{   

    public string Mm1 { get; set; }

    public string Mm2 { get; set; }

}

自动映射器代码


spt_detail details = db.spt_detail.ToList();


Mapper.Initialize(n => n.CreateMap<List<spt_detail>, List<spt_detail_extended>>());

List<spt_creance_detail_enrichi> cenr = AutoMapper.Mapper.Map<List<spt_detail>, List<spt_detail_enrichi>>(details);

问题:详细信息包含 8 行,但 cenr 0。


有人可以帮助我吗?


互换的青春
浏览 138回答 3
3回答

米脂

将spt_detail映射到spt_detail_extended。您应该仅为模型创建映射规则,如下所示:Mapper.Initialize(cfg&nbsp;=> { &nbsp;&nbsp;&nbsp;&nbsp;cfg.CreateMap<spt_detail,&nbsp;spt_detail_extended>(); });之后,您应该使用以下结构:List<spt_detail_extended>&nbsp;extendeds&nbsp;=&nbsp;Mapper.Map<List<spt_detail_extended>>(details);如果您想映射其他模型,只需添加或编辑您的配置即可。

PIPIONE

不要映射列表,而是像这样映射:Mapper.Initialize(n&nbsp;=>&nbsp;n.CreateMap<spt_detail,&nbsp;spt_detail_extended>());你打电话去做地图会保持不变:List<spt_detail_extended>&nbsp;cenr&nbsp;=&nbsp;AutoMapper.Mapper.Map<List<spt_detail>,&nbsp;List<spt_detail_extended>>(details);

郎朗坤

你在这里错过了两个步骤。第一个是您需要初始化业务对象的列表,而不是仅初始化单个业务对象。您从数据库中检索列表(使用 .Tolist())这是我如何初始化对象的示例:List <SptDetail> details = new List <SptDetail> {&nbsp;new SptDetail {&nbsp; Age = "10",&nbsp; &nbsp;Name = "Marion"&nbsp;},&nbsp;new SptDetail {&nbsp; Age = "11",&nbsp; &nbsp;Name = "Elisabeth"&nbsp;}};第二个失误是您正在映射 lits,我建议您使用单个业务类对象,如下所示:Mapper.Initialize(n => n.CreateMap<SptDetail, SptDetailExtended>()&nbsp;.ForMember(obj => obj.ExProp1, obj => obj.MapFrom(src => src.Name))&nbsp;.ForMember(obj => obj.ExProp2, obj => obj.MapFrom(src => src.Age)));而洞故事的关键是使用。ForMember指定 wich 成员的位置,因为属性不具有相同的名称。这是一个代码示例,运行起来就像一个魅力:internal class Program{&nbsp; &nbsp; public static List<SptDetailExtended> InitializeExtendedObjects()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var details = new List<SptDetail>&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new SptDetail&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Age = "10",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name = "Marion"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new SptDetail&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Age = "11",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name = "Elisabeth"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; //this is wrong db.spt_detail.ToList();&nbsp; &nbsp; &nbsp; &nbsp; Mapper.Initialize(n => n.CreateMap<SptDetail, SptDetailExtended>()&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /*you need to use ForMember*/ .ForMember(obj => obj.ExProp1, obj => obj.MapFrom(src => src.Name))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ForMember(obj => obj.ExProp2, obj => obj.MapFrom(src => src.Age)));&nbsp; &nbsp; &nbsp; &nbsp; //instead of this Mapper.Initialize(n => n.CreateMap<List<spt_detail>, List<spt_detail_extended>>());&nbsp; &nbsp; &nbsp; &nbsp; //change your mapping like following too&nbsp; &nbsp; &nbsp; &nbsp; var cenr = Mapper.Map<List<SptDetailExtended>>(details);&nbsp; &nbsp; &nbsp; &nbsp; return cenr;&nbsp; &nbsp; }&nbsp; &nbsp; private static void Main(string[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var result = InitializeExtendedObjects();&nbsp; &nbsp; &nbsp; &nbsp; foreach (var sptDetailExtended in result)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(sptDetailExtended.ExProp1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(sptDetailExtended.ExProp2);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Console.ReadLine();&nbsp; &nbsp; }}希望这可以帮助!
打开App,查看更多内容
随时随地看视频慕课网APP