Search code examples
c#entity-frameworkasp.net-coreentity-relationshipfluent

How to define one list property that maps to multiple one to manys of shared super type in fluent ef core?


First I have a base class like this:

public class BaseModel
{
      public string Information { get; set; }
}

Then I have two sub classes of the base class:

public class SubType1 : BaseModel
{
    public int Id { get; set; }

    public int TestData1 { get; set; }

    public int ParentId { get; set; }

    public virtual Parent Parent { get; set; }
}


public class SubType2: BaseModel
{
    public int Id { get; set; }

    public string TestData2 { get; set; }

    public int ParentId { get; set; }

    public virtual Parent Parent { get; set; }
}

Then I have a parent class that has a list of the base class:

public class Parent
{
    public int Id { get; set; }

    public virtual List<BaseModel> Components { get; set; } = new List<BaseModel>();
}

The Parent entity has many SubType1s and then also many SubType2s

SubType1 and SubType2 each have their own table

What is some fluent I could add that would automatically make the Components list on the Parent entity return all entites from the SubType1 and SubType2 tables?

Key requirement: Needs to be in fluent


Solution

  • According to your description, it seems that you are using the Table per Concrete Type (TPC), right?

    As far as I know, in the EF core, the TPC pattern is currently on the backlog, which means that it is being considered for inclusion as a feature, but no date has been set as yet.