首页 文章

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

提问于
浏览
0

我使用代码优先方法在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

    声明一个定义该表的类:

    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
    }
    

相关问题