c# - Map List<> with Automapper? -
i have 2 classes:
public class customerdto { public string name {get;set;} public list<order> orders {get;set;} } public class orderdto { public string name {get;set;} public string description {get;set;} public decimal cost{get;set;} }
i using automapper .net 3.5 , doing following in application_startup:
mapper.createmap<customer, customerdto>(); mapper.createmap<order,orderdto>();
this simplified example named dto properties different entity properties, used formember, unclear on how map orders customer:
i tried:
mapper.createmap<customer, customerdto() .formember(dest => dest.orders, opt=> opt.mapfrom(src=>src.orders));
but not find src.orders
.
if indeed need have both createmap
statements, automapper
"automatically" link objects customer
orders
?
yes, need tell automapper each mapping. not guess you. so, if orderdto
should map order
, must tell automapper that. must specify reverse relationship if that's needed (i.e. order
should map orderdto
).
in other words, bi-directional mapping need:
mapper.createmap<order, orderdto>(); mapper.createmap<orderdto, order>();
as far customer
goes, if both customer
, customerdto
have property named orders
, don't need else. long you've told automapper map between order
, orderdto
, customer
, customerdto
, automatically map order
when map customer
.
Comments
Post a Comment