c# - resolving complex entity model to flat model view using AutoMapper -
i want create map complex entity model flattened view model
my entity model so
cbitems has many cbitemscontent has many cbregulators
so viewmodels so
for cbitems:
public class itemviewmodel { public itemviewmodel() { this.cbitemscontents = new hashset<itemcontentviewmodel>(); } public int itemid { get; set; } ...... public virtual icollection<itemcontentviewmodel> cbitemscontents { get; set; } } }
for cbitemscontent:
public class itemcontentviewmodel { public int itemcontentid { get; set; } public int itemid { get; set; } .... public itemcontentregulatorsviewmodel regulatedby { get; set; } } }
for cbregulators:
public class itemcontentregulatorsviewmodel { public int itemcontentid { get; set; } public ienumerable<int> regulatorids { get; set; } } }
i had hoped easy this:
config.createmap<cbitem, itemviewmodel>(); config.createmap<cbitemscontent, itemcontentviewmodel>() .formember(dest => dest.regulatedby.itemcontentid, m => m.mapfrom(src => src.genericid)) .formember(dest => dest.regulatedby.regulatorids, n => n.mapfrom(src => src.cbitemscontentregulators.select(q => q.cbregulator.regulatorid)));
from teh following query:
itemviewmodel item = _context.cbitems.where(u => u.itemid = id) .projectto<itemviewmodel>() .first();
but results in error:
expression 'dest => dest.regulatedby.itemcontentid' must resolve top-level member , not child object's properties. use custom resolver on child type or aftermap option instead. parameter name: lambdaexpression
how can achieve desired model layout?
@rabban means this:
config.createmap<cbitemscontent, itemcontentviewmodel>() .formember(dest => dest.regulatedby, o => o.mapfrom(src => src)); config.createmap<cbitemscontent, itemcontentregulatorsviewmodel>() .formember(dest => dest.itemcontentid, o => o.mapfrom(src => src.genericid)) .formember(dest => dest.regulatorids, o => o.mapfrom(src => src.cbitemscontentregulators.select(q => q.cbregulator.regulatorid)));
Comments
Post a Comment