0

I have the following problem. I'm building TPH inheritance with Entity Framework and I need to set discriminator column on existing property of subclass. Example:

public abstract class Building 
{
    //... some properties
    public BuildingType BType { get; set; } // sub class with discriminator property
}

public class BuildingA : Building
{

}

public class BuildingB : Building
{

}

public class BuildingType
{
    //... some properties
    public string Category { get; set; } 
    /*
    discriminator property
    if this property is set to "A" then the building is type BuildingA
    and if is set to "B" then the building is type BuildingB
    */
}

So - every building has required the BuildingType property and the BuildingType class has the property Category (which already exists) and which can be disciminator for the buildings TPH inheritance. How can I achieve this?

chobotek
  • 77
  • 2
  • 6
  • As far as I understand, you have classes BuildingA and BuildingB derived from Building. Then discrimination will be done by the type of the concrete Building instance, not by a value of its property. Basically it achieves the same effect you wanted. See this answer: http://stackoverflow.com/a/29984502/4544845 – felix-b Apr 09 '17 at 22:39
  • @chobotek - just to add to what felix said, the string value is just a way to reflect the type of the building in the database table. While querying against your model in EF, you have to provide the type of the derived class (Which is the derived class itself), and not the string value.... – gkb Apr 10 '17 at 06:00
  • It's all about the setting of discriminator column. I know how to set discriminator column in parent class table (table of buildings in this case), but I need to set this discriminator column (category) in buildingTypes table instead. Every building has joined one buildingType entity and when I choose random BuildingType I know exactly derived class (BuildingA or BuildingB) of buildings which are joined to this BuildingType. – chobotek Apr 10 '17 at 12:09

0 Answers0