William
William

Reputation: 1384

Table per Type inheritance in Entity Framework 7 beta 4

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.

Upvotes: 0

Views: 2680

Answers (1)

bricelam
bricelam

Reputation: 30345

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; }
}

Upvotes: 3

Related Questions