0

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.

1 Answer 1

3

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; }
}
Sign up to request clarification or add additional context in comments.

3 Comments

looks like TpT has been pushed back, and will not be in the first release. hopefully it will come fairly quickly after release.
Parent is never used here... Did you mean to have public Parent Parent in the Person class?
@bricelam thanks for the update. Any news? I'm about to start a new ASP.NET project, that TPT is a requirement. My question is can I use ASP.NET Core with the EF6? Then how about the Microsoft.AspNet.Identity.EntityFramework package? is there any official NuGet based alternative?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.