【问题标题】:Entity Framework Code First: Entity Splitting实体框架代码优先:实体拆分
【发布时间】:2012-09-28 17:54:05
【问题描述】:

是否可以映射以下场景?

数据表

学生

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

类

+ ID:int PK
+ 学生 ID:FK
+ CourseID:FK
+ 注册日期:日期时间

课程

+ 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
}

【问题讨论】:

    标签: frameworks entity


    【解决方案1】:

    这应该是建立你需要的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;}
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-01
      • 2014-05-23
      • 1970-01-01
      • 2016-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多