0

是否可以映射以下场景?

数据表

学生

+ ID:int PK
+ 名称:varchar(200)

课程

+ ID:int PK
+ StudentID:FK
+ CourseID:FK
+ EnrollmentDate:DateTime

培训班

+ ID:int PK
+ 名称:varchar(200)

我想将表格映射到下面的实体。

public class Student
{
    [Key]
    public int ID {get;set;}
    public string Name {get;set;}
    public virtual ICollection<Class> Classes {get;set;}
}

public class Class
{
[Key]
public int ID {get;set;}
public Student Student {get;set;}
public DateTime EnrollmentDate {get;set;}
public string Name {get;set;} // this comes from the Courses data table
}

4

1 回答 1

0

这应该是如何建立您需要的 FK 关系:

public class Student
{
   [Key]
   public int StudentId {get;set;}
   public string Name {get;set;}
}

public class Class
{
   [Key]
   public int ClassId {get;set;}
   public DateTime EnrollmentDate {get;set;}

   //foreign keys
   public int CourseId {get;set;}
   public string StudentId {get;set;} 

   //virtual constraint to link to referenced tables
   public virtual Course Course {get;set;}
   public virtual Student Student {get;set;}
}

public class Course
{
   [Key]
   public int CourseId {get;set;}
   public string CourseName {get;set;}
}
于 2013-07-11T19:55:52.853 回答