Entity framework 使用每个层次结构的表(TPH)时的代码优先映射问题
在过去的几天里,我们首先开始编写代码,作为其中的一部分,我在进行映射时遇到了一个问题,我在这里用不同的示例总结了这个问题。如果我能找到解决办法,那会更有帮助 为此 以下是三个实体 课程->实体持有课程信息,如费用、说明等,每门课程将 属于某个类别或类型(即学生、员工等),它指的是在家长ID和类型栏的帮助下进行注册的人 示例:要获取ID为10的学生参加的课程,sql查询将Entity framework 使用每个层次结构的表(TPH)时的代码优先映射问题,entity-framework,ef-code-first,Entity Framework,Ef Code First,在过去的几天里,我们首先开始编写代码,作为其中的一部分,我在进行映射时遇到了一个问题,我在这里用不同的示例总结了这个问题。如果我能找到解决办法,那会更有帮助 为此 以下是三个实体 课程->实体持有课程信息,如费用、说明等,每门课程将 属于某个类别或类型(即学生、员工等),它指的是在家长ID和类型栏的帮助下进行注册的人 示例:要获取ID为10的学生参加的课程,sql查询将 "select * from course where type=1 and parentid=10" Student:此
"select * from course where type=1 and parentid=10"
Student:此处的课程加入实体应仅获取Student(即..1)类型的相关记录,同样适用于员工
那么首先,如何实现这一目标呢
我试图在这里实现TPH逻辑,如下所示,但我遇到了异常
public enum CourseType
{
Student = 1,
Employee = 2
}
public class Course
{
public int ID { get; set; }
public int ParentID { get; set; }
public decimal Amount { get; set; }
public CourseType Type { get; set; }
public string Description { get; set; }
}
public class StudentCourse : Course
{
}
public class EmployeeCourse : Course
{
}
public class Student
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<StudentCourse> CourseJoined { get; set; }
}
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public string WorkingAs { get; set; }
public string Experiance { get; set; }
public virtual ICollection<EmployeeCourse> CourseJoined { get; set; }
}
如果为课程创建了两列,但我不需要两列(t.ParentID和t.ParentID),我只需要ParentID,在其中插入学生和员工id
有谁能指导我修复上述问题,或者提出任何建议来实现上述场景
我使用的是EF6.0版本
CreateTable(
"dbo.Course",
c => new
{
ID = c.Int(nullable: false, identity: true),
Amount = c.Decimal(nullable: false, precision: 18, scale: 2),
Type = c.Int(),
Description = c.String(),
ParentID = c.Int(),
ParentID1 = c.Int(),
})
.PrimaryKey(t => t.ID)
.ForeignKey("dbo.Employee", t => t.ParentID, cascadeDelete: true)
.ForeignKey("dbo.Student", t => t.ParentID1, cascadeDelete: true)
.Index(t => t.ParentID)
.Index(t => t.ParentID1);