简体   繁体   English

实体框架数据库优先-模型中的继承问题

[英]Entity Framework DB First - Problems with inheritance in the model

I'm facing some problems working with EF DB First. 我在使用EF DB First时遇到一些问题。 Say I've got: 说我有:

  • A table Person 一桌人
  • A table Student with a foreign key pointing to Person 带有外键指向人的表学生
  • A table Teacher with a foreign key pointing to Person 带有外键指向人的表老师

数据库结构

The model created from the database generates the next classes: 从数据库创建的模型将生成以下类:

public class Person{
  this.Student= new HashSet<Student>();
  this.Teacher= new HashSet<Teacher>(); 
}

public class Student{}
public class Teacher{}

And what I'd really like to see is 我真正想看到的是

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

Is there any convention over configuration or anything I'm missing to get the inherited classes ? 是否有关于配置的约定,或者我缺少获得继承的类的任何约定?

UPDATE 更新

Classes are generated in such a way because the model specifies these associations between Person, Teacher and Student. 之所以生成类,是因为该模型指定了“人”,“老师”和“学生”之间的这些关联。 My question should be then... Is there any way to create a model from a DB using EF so that the model contains classes that inherit from other ones? 那么我的问题应该是... 有没有办法使用EF从数据库创建模型,以便该模型包含从其他类继承的类?

You're looking for Table-per-Type (TPT) mapping. 您正在寻找每类型表格(TPT)映射。 Try the solution here: 在这里尝试解决方案:

Entity Framework DB-First, implement inheritance 实体框架DB-First,实现继承

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM