[英]Multiple level inheritance. Entity Framework C#
I have been working with inheritance on Entity Framework 6 (code-first) with Visual Studio 2015. At this point I wanted to try Multiple Inheritance like this (This is a summary not the exactly syntaxt): 我一直在使用Visual Studio 2015在实体框架6(代码优先)上处理继承。在这一点上,我想尝试像这样的多重继承(这是一个摘要,而不是确切的语法):
public abstract class Person {
public String Name
public String LastName
}
public class Teacher : Person {
[Key]public int Id_Teacher
}
public class Student : Person {
[Key] public int Id_Student
public string code_s
}
public class ExchangeStudent : Student {
[Key] public int Id_ExchangeStud
public string HomeUniversity
}
I have made the first step that is create Person and the Child tables Teacher & Student, but when it comes to create the third child table it don't work. 我迈出的第一步是创建Person和Child表Teacher&Student,但是创建第三个child表却无法正常工作。
I used TPC for the first step so in the Context I got DbSet of Students and Teachers. 我第一步使用了TPC,因此在上下文中,我获得了DbSet的学生和教师。
Is there any way to implement the third table EXCHANGE STUDENT?? 有什么方法可以实现第三个表EXCHANGE STUDENT?
Thanks you so much. 非常感谢。
If I understand correctly, your Model design should look like below, 如果我理解正确,则您的模型设计应如下所示,
public class Person
{
[key] //No need to mention [key] annotation here, Because EF will automatically understand Id property will act as Primary Key.
public int Id { get; set; }
public string Name { get; set; }
public string LastName { get; set; }
}
public class Teacher: Person
{
}
public class Student: Person
{
public string Code { get; set; }
}
public class ExchangeStudent : Student
{
public string HomeUniversity { get; set; }
}
You have to avoid each [Key] properties in each child classes . 您必须避免每个子类中的每个[Key]属性。 B ase Class will have an Id property which will act as a PrimaryKey for the Table and all other child class.
Base类将具有一个Id属性,该属性将用作Table和所有其他子类的PrimaryKey。
If you follow the above, after applying the migration script into the Table, System will create a Table(Persons) with Discriminator column for the child class. 如果您按照上述步骤操作,则将迁移脚本应用到表中后,系统将为子类创建一个表(人员)和区分项列。
Hope this may help you to move forward! 希望这可以帮助您前进!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.