asp.net - EF code first - map child class property to base class table -
i wonder if it's possible map child class property base class table. have 2 classes (shortened):
public abstract class user { [key] public int userid { get; set; } public string username { get; set; } // other properties... }
and
public class customer : user { public int shopid { get; set; } public virtual shop shop { get; set; } // other properties... }
i'm using tpt (table per type) inheritance (that means 2 tables - user , customer). reasons have shopid
property in user table, other properties customer
class in customer table. possible?
having shopid column in user table allow example create unique index on username , shopid (the application multi-tenant don't want globally unique usernames, shop-level unique usernames).
is you're looking for?
userbase.cs
public abstract class userbase { public int userid { get; set; } public string username { get; set; } public int shopid { get; set; } public virtual shop shop { get; set; } }
user.cs
public class user : userbase { // user specific properties... }
customer.cs
public class customer : userbase { // customer specific properties... }
userdbcontext.cs
public class userdbcontext : dbcontext { ... protected override onmodelcreating(dbmodelbuilder modelbuilder) { // if want users , customers shop specific modelbuilder.entity<userbase>.haskey(x => new { x.userid, x.shopid }); // if want users shop specific uncomment below , remove above //modelbuilder.entity<user>.haskey(x => new { x.userid, x.shopid }); } }
Comments
Post a Comment