Sto utilizzando Entity Framework 7 Beta 4 e devo mappare su un database esistente.
Il database utilizza una gerarchia Table per Type simile a questo articolo ( http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/implementing-inheritance-with-the -entity-framework-in-un-asp-net-mvc-applicazione )
Qui c'è un semplice esempio:
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; }
}
Quando provo a interrogare DbSet, ottengo questo errore:
L'espressione '[100001] .Persone' passata all'operatore Includi non può essere associata.
Stiamo ancora lavorando sull'eredità. (Vedere il numero 247. ) TPH / STI è parzialmente implementato, ma non TPT. Per ora, potresti fare una mappatura diretta tra le tabelle e le classi. Qualcosa di simile a:
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; }
}