问题在多对多关系实体框架代码中的第一种方法

时间:2014-02-24 05:15:33

标签: entity-framework entity-framework-4 entity-framework-5

我使用代码优先方法在entityframework中创建了一个应用程序。

在应用程序中,有两个实体之间有多对多的关系。

public class Course
    {

        [Key]
        public int CourseId { get; set; }
        public string Name { get; set; }

        public ICollection<Student> Student{ get; set; }
    }


   public class Student
    {

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

        public ICollection<Course> Course{ get; set; }
    }

当我执行项目时,它会创建学生表,课程表,StudentCourse表。

现在问题是,在StudentCourse表中只有两个键,StudentID和CourseId 我想在该表中添加其他列如何做到这一点?

1 个答案:

答案 0 :(得分:0)

声明一个定义该表的类:

public class Course
{

    [Key]
    public int CourseId { get; set; }
    public string Name { get; set; }

    public ICollection<Student> Student{ get; set; }
}

public class Student
{

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

    public ICollection<Course> Course{ get; set; }
}

public class StudentCourse
{

    public int StudentId { get; set; }
    public int CourseId { get; set; }

    //More columns here
}