entity framework 5 - C# .NET 4.5 and EF 5 update navigation property (foreign key) -
i can't update navigation property have 2 entities: user
public class user : entity { public string username { get; set; } public string password { get; set; } public int? addressid { get; set; } public virtual address address { get; set; } }
and address
public class address : entity { public string addressname { get; set; } public int userid { get; set; } public virtual user user { get; set; } }
when trying following:
address.user = user;
address userid updates succesfully, , in commit method, entity state modified. users addressid won't update.
here context
public dbset<user> users { get; set; } public dbset<address> addresses { get; set; } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.entity<user>() .hasoptional(x=>x.address) .withmany() .hasforeignkey(x=>x.addressid) .willcascadeondelete(false); base.onmodelcreating(modelbuilder); }
and using unity framework , genereic repository pattern. tried set properties virtual no effect.
i think there conception problem here, beyond foreign key problem. why refer addressid
if have address
object in user
class ? it's same thing address
class. maybe should try :
public class user : entity { public string username { get; set; } public string password { get; set; } public virtual address address { get; set; } } public class address : entity { public string addressname { get; set; } public virtual user user { get; set; } }
but make clear, entity framework not magic wizard (clause to...). architecture, have manually update both foreign keys (if want keep addressid , userid properties).
Comments
Post a Comment