Table per Type inheritance in Entity Framework 7 beta 4

asp.net-core entity-framework entity-framework-core

Question

I'm using Entity Framework 7 Beta 4 and need to map to an existing database.

The database uses a Table per Type hierarchy similar to this article (http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/implementing-inheritance-with-the-entity-framework-in-an-asp-net-mvc-application)

Here is a simple example:

public abstract class Person
{
    // SQL Table: Person
    // SQL Columns: PersonId, Name
    public long PersonId { get; set; }
    public string Name { get; set; }
    // This column would contain 'STUDENT' or 'PARENT'
    public string PersonType { get; set; }
}

public class Student : Person
{
    // SQL Table: Person_Student
    // SQL Columns: PersonId, GPA
    public decimal GPA { get; set; }
}

public class Parent : Person
{
    // SQL Table: Person_Parent
    // SQL Columns: PersonID, EmergencyContactNumber
    public string EmergencyContactNumber { get; set; }
}

public class PersonTestDb : DbContext
{
    public DbSet<Person> People { get; set; }
}

When trying to query the DbSet, I get this error:

The expression '[100001].People' passed to the Include operator could not be bound.

Accepted Answer

We're still working on inheritance. (See issue #247.) TPH/STI is partially implemented, but not TPT. For now, you could do a direct mapping between the tables and classes. Something like:

class Person
{
    // Null when Parent
    public Student Student { get; set; }

    // Null when Student
    public Parent Parent { get; set; }
}

class Student
{
    public int PersonId { get; set; }
    public Person Person { get; set; }
}

class Parent
{
    public int PersonId { get; set; }
    public Person Person { get; set; }
}


Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why