Question

I have these three models:

public class Equipment
{
    public int ID { get; set; }
    public string title { get; set; }

}

[Table("Vessels")]
public class Vessel:Equipment
{
    public string Size { get; set; }

}

[Table("Tubes")]
public class Tube : Equipment
{
    public string Pressure{ get; set; }

}

I want to show a list of Equipments with 2 columns title and type.

for example:

Title        Type
------       -------
101-1        vessel
101-2        vessel
102-3        tube

I don't know how to make a discriminator column in Equipments to show the type of each equipments.

EDITED

If I have a discriminator in Equipment entity like:

public class Equipment
{
    public int ID { get; set; }
    public string title { get; set; }
    public string type{ get; set; }  //as discriminator
}

I can get the query in controller or repository like this:

var equipments=from e in db.Equipments
               select e;
Was it helpful?

Solution

You cannot make discriminator column in terms of EF mapping - TPT inheritance doesn't support it because the discriminator is a subtable. You can try to use something like:

public abstract class Equipment
{
    public int ID { get; set; }
    public string title { get; set; }

    [NotMapped]
    public abstract string Type { get; }
}  

and override Type property in subtypes to get the correct name. You will not be able to use that property in Linq-to-Entities queries because it is not mapped.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top