c# - Split table in Entity Framework into multiple entities -
here's problem. have table user have quite few fields. want split table multiple entities this:
user -> generaldetails -> communicationdetails -> address etc.
all goes when extracting fields user generaldetails. however, when try same thing communicationdetails ef blows , require establish one-to-one relationship between generaldetails , communicationdetails.
sample entities definition:
public class user { public int userid { get; set; } public string somefield1 { get; set; } public int somefield2 { get; set; } public virtual generaldetails generaldetails { get; set; } public virtual communicationdetails communicationdetails { get; set; } public virtual address address { get; set; } } public class generaldetails { [key] public int userid { get; set; } public string firstname { get; set; } public string lastname { get; set; } public string email { get; set; } public virtual user user { get;set; } } public class communicationdetails { [key] public int userid { get; set; } public string phone { get; set; } public string devicetoken { get; set; } public virtual user user { get;set; } } public class address { [key] public int userid { get; set; } public string city { get; set; } public string country { get; set; } public string street { get; set; } public virtual user user { get;set; } }
sample mapping:
modelbuilder.entity<user>(). hasrequired(user => user.generaldetails). withrequiredprincipal(details => details.user); modelbuilder.entity<user>(). hasrequired(user => user.communicationdetails). withrequiredprincipal(details => details.user); modelbuilder.entity<user>(). hasrequired(user => user.address). withrequiredprincipal(details => details.user); modelbuilder.entity<user>().totable("users"); modelbuilder.entity<generaldetails>().totable("users"); modelbuilder.entity<address>().totable("users");
why on earth ef want relationship? there way solved?
the correct way complex types rather entities. more common problem think.
public class mydbcontext : dbcontext { protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.complextype<communicationdetails>(); modelbuilder.complextype<generaldetails>(); modelbuilder.complextype<address>(); modelbuilder.entity<user>().totable("users"); } }
Comments
Post a Comment