使用继承时TPT添加哪个DbSet

时间:2016-02-04 17:51:18

标签: .net inheritance entity-framework-6 table-per-type

假设我有以下实体:

public class Person { ... }
public class Student : Person { ... }
public class Teacher : Person { ... }

在上下文类中,以下哪个更好(以及为什么?)

  • 仅创建DbSet<Person> Persons { get; set; }并使用context.Persons.OfType<Student>()
  • 检索学生
  • 创建3个DbSet s(DbSet<Person>DbSet<Student>DbSet<Teacher>)或
  • 仅针对子类型(DbSetDbSet<Student>)创建2 DbSet<Teacher>

谢谢!

1 个答案:

答案 0 :(得分:0)

仅创建DbSet<Person> Persons { get; set; }并按如下方式修饰继承类

[Table("Students")]
public class Student : Person { ... }
[Table("Teachers")]
public class Teacher : Person { ... }

参考:http://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-2-table-per-type-tpt