c# - Entity Framework Inheritance Mapping (TPH) -
i'm working on converting project nhibernate entity framework , stuck on mapping issue mapping inheritance.
i have next base of operations class (shortened brevity):
public abstract class status { public guid id { get; set; } public string name { get; set; } } public class projectstatus : status { } public class taskstatus : status { } can point me in right direction how write mappings taskstatus , projectstatus inherit status? utilize table-per-hierarchy (all saved in 1 table using discriminator)
table per hierarchy default. shouldn't need provide additional setup besides have.
this taken tph illustration weblogs.asp.net:
public abstract class billingdetail { public int billingdetailid { get; set; } public string owner { get; set; } public string number { get; set; } } public class bankaccount : billingdetail { public string bankname { get; set; } public string swift { get; set; } } public class creditcard : billingdetail { public int cardtype { get; set; } public string expirymonth { get; set; } public string expiryyear { get; set; } } public class inheritancemappingcontext : dbcontext { public dbset<billingdetail> billingdetails { get; set; } } this create single table called billingdetails discriminator column.
discriminator column
as can see in db schema above, code first has add together special column distinguish between persistent classes: discriminator. isn’t property of persistent class in our object model; it’s used internally ef code first. default, column name "discriminator", , type string. values defaults persistent class names —in case, “bankaccount” or “creditcard”. ef code first automatically sets , retrieves discriminator values.
c# entity-framework
No comments:
Post a Comment