entity framework - Automapper maps source to destination but dest values are always null -
i'm new automapper , i'm having problem it. in case automapper used map models(entityframework generated) own viewmodels. happens, sourcemodel it's values mapped destinationmodel dest values null. what's going on values?
now did do: referenced automapper project , bootstrapped mappings.
public static void registerautomappermappings() { mapper.initialize(x => { // add mappingprofiles configured below x.addprofile(new registrationviewmodelprofile()); }); } public static imappingexpression<tsource, tdest> ignoreallunmapped<tsource, tdest>(this imappingexpression<tsource, tdest> expression) { expression.forallmembers(opt => opt.ignore()); return expression; } public class registrationviewmodelprofile : profile { protected override void configure() { createmap<registrationviewmodel, contact>().ignoreallunmapped(); createmap<contact, registrationviewmodel>().ignoreallunmapped(); createmap<registrationviewmodel, emailaddress>().ignoreallunmapped(); createmap<emailaddress, registrationviewmodel>().ignoreallunmapped(); createmap<registrationviewmodel, password>().ignoreallunmapped(); createmap<password, registrationviewmodel>().ignoreallunmapped(); //always check if mapping valid mapper.assertconfigurationisvalid(); } }
my viewmodel:
public class registrationviewmodel { public httppostedfilebase file { get; set; } public string emailaddress { get; set; } public string password { get; set; } public string contact_givenname { get; set; } public string contact_surname_prefix { get; set; } public string contact_surname { get; set; } public string contact_gender { get; set; } public string contact_country { get; set; } public string contact_residence { get; set; } public nullable<datetime> contact_birth_date{ get; set; } public datetime create_date { get; set; } public icollection<int> contact_roles { get; set; } public string emailaddress_verificationkey { get; set; } }
my model:
public partial class contact { public contact() { this.contact_connection_rel = new hashset<contact_connection_rel>(); this.contact_emailaddress_password_rel = new hashset<contact_emailaddress_password_rel>(); this.contact_emailaddress_rel = new hashset<contact_emailaddress_rel>(); this.contact_service_role_rel = new hashset<contact_service_role_rel>(); this.given_answer = new hashset<given_answer>(); this.given_answer1 = new hashset<given_answer>(); } public int contact_id { get; set; } public string contact_initials { get; set; } public string contact_givenname { get; set; } public string contact_surname_prefix { get; set; } public string contact_surname { get; set; } public string contact_nickname { get; set; } public string contact_gender { get; set; } public nullable<system.datetime> contact_birth_date { get; set; } public string contact_country { get; set; } public string contact_residence { get; set; } public string contact_ssn { get; set; } public nullable<system.datetime> create_date { get; set; } public nullable<system.datetime> modify_date { get; set; } public nullable<system.datetime> delete_date { get; set; } public virtual icollection<contact_connection_rel> contact_connection_rel { get; set; } public virtual icollection<contact_emailaddress_password_rel> contact_emailaddress_password_rel { get; set; } public virtual icollection<contact_emailaddress_rel> contact_emailaddress_rel { get; set; } public virtual icollection<contact_service_role_rel> contact_service_role_rel { get; set; } public virtual icollection<given_answer> given_answer { get; set; } public virtual icollection<given_answer> given_answer1 { get; set; } }
and test configuration following lines used. vars contain destination objects null:
contact c = new contact(); contact testc = unitofwork.contactrepository.find(82); var x = mapper.map<contact, registrationviewmodel>(testc); var y = mapper.map(regmodel, c, typeof(registrationviewmodel), typeof(contact)); var b = mapper.dynamicmap<registrationviewmodel, contact>(regmodel); var z = mapper.map<registrationviewmodel, contact>(regmodel, c); var w = mapper.map<registrationviewmodel, contact>(regmodel);
expression.forallmembers(opt => opt.ignore());
you're telling automapper ignore properties, nothing gets mapped.
if want ignore non-matching properties, see this answer 1 way, otherwise you're going have explicitly map each property between objects.
Comments
Post a Comment