c# - Entity Framework 6 - Code first - FK is generated in inherited classes but relation is defined in base class -
what want have base class , 2 separate lists of inherited classes. model:
public class instance { public int instanceid { get; set; } public virtual icollection<user> users { get; internal set; } public virtual icollection<masteruser> masterusers { get; internal set; } } public abstract class coreuser { [key] public int userid { get; set; } public int instanceid { get; set; } public virtual instance instance { get; set; } } [table("users")] public class user : coreuser { public string username { get; set; } } [table("masterusers")] public class masteruser : coreuser { public string masterusername { get; set; } }
this dbcontext:
public class mycontext : dbcontext { public dbset<user> users { get; set; } public dbset<masteruser> masterusers { get; set; } public dbset<instance> instances { get; set; } }
this create 4 tables tpt inheritance model fine. problem users , masterusers table contain foreign key instance (it called instance_instanceid) redundant since fk defined in coreuser base class. these 2 fk columns not populated, null, coreusers instanceid column populated when add either user or masteruser.
if remove both referenced instance class so:
public class instance { public int instanceid { get; set; } }
problem goes away renders application unusable. can solve problem so:
public class instance { public int instanceid { get; set; } public virtual icollection<coreuser> users { get; internal set; } }
and iterate trough collection filtering out each type of user approach lazy load of users though want iterate trough masterusers only.
one possible solution use tpc in reality, coreuser class contain fks other classes not supported in tpc (only top level classes in hierarchy can contain fks). there way can work in ef using 2 separate lists in instance class , have them lazy loaded?
edit
actually, above code work fine. break if introduce 1 more class references coreuser example:
public class userpolicy { public int policyid { get; set; } public virtual coreuser policyuser { get; internal set; } }
managed around this. solution able use move relationship between coreuser , instance user , masteruser so:
public class instance { public int instanceid { get; set; } // still referencing 2 lists public virtual icollection<user> users { get; internal set; } public virtual icollection<masteruser> masterusers { get; internal set; } } public abstract class coreuser { [key] public int userid { get; set; } // no reference instance. works if don't need coreuser } [table("users")] public class user : coreuser { // fk instance defined in coreuser public int instanceid { get; set; } public virtual instance instance { get; set; } public string username { get; set; } } [table("masterusers")] public class masteruser : coreuser { // fk instance defined in masteruser public int instanceid { get; set; } public virtual instance instance { get; set; } public string masterusername { get; set; } }
Comments
Post a Comment