c# - Multiple Inheritance Levels Using Entity Framework TPT -
considering following table schema:
i want use entity framework table-per-type (fluent api) map following class diagram:
how can this? have tried doing examples @ microsoft site looks no example covers two-depth inheritance scenario , errors related invalid discriminator columns , on. using 1 entitytypeconfiguration
per type. how can specify name of foreign key cascading down children?
if has example of how such mapping, grateful forever.
thanks!
so table per type won't quite work schema because table per type links tables through shared primary key. modify schema useraccounts, externaluseraccounts, , systemuseraccounts keyed on useraccountid. code below close want. otherwise, you'll want use regular association properites.
read table per type here , shared primary keys here.
public class useraccount { public int useraccountid {get;set;} public int userid {get;set;} public datetime lastlogin {get;set;} public bool islocked {get;set;} public bool isactive {get;set;} public user user {get;set;} } public class externaluseraccount : useraccount { public int externalaccountid {get;set;} public string providername {get;set;} public string providerusername {get;set;} } public class systemuseraccount : useraccount { public int systemuseraccountid {get;set;} public string passwordhash {get;set;} public string token {get;set;} } public class user { public int userid {get;set;} public string name {get;set;} public string description {get;set;} } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.entity<useraccount>().totable("useraccounts"); modelbuilder.entity<useraccount>().haskey(ua => ua.useraccountid); modelbuilder.entity<useraccount>().hasrequired(ua => ua.user) .withmany() .hasforeignkey(ua => ua.userid); modelbuilder.entity<externaluseraccount>().totable("externaluseraccounts"); modelbuilder.entity<systemuseraccount>().totable("systemuseraccounts"); modelbuilder.entity<user>().totable("users"); modelbuilder.entity<user>().haskey(u => u.userid); }
Comments
Post a Comment