在代码第一现有数据库中更新实体框架中的多对多关系中的数据

时间:2022-10-05 15:09:10

I am working on app and struggling to really understand how to update data in tables where we have intermediate join table (i.e. table to break many-to-many relationship). for example, from following diagram. if I say I want to add record for new student with list of three courses i.e. math, English and computing. how I do that where I have

我正在研究应用程序,并努力真正理解如何在我们有中间连接表(即打破多对多关系的表)的表中更新数据。例如,从下图。如果我说我想为新学生添加记录,包括数学,英语和计算三门课程。我是怎么做到的

public virtual ICollection<StudentCourse> StudentCourses { get; set; } 

在代码第一现有数据库中更新实体框架中的多对多关系中的数据

another questions; another scenario if I have courses already and of-course I don't want duplication of math, English and computing course title, how I add new instance of student record there??

另一个问题;另一种情况,如果我已经有课程,当然我不想重复数学,英语和计算机课程标题,我如何添加新的学生记录实例?

Student

 public partial class Student
 {
    public Student()
    {
        this.StudentCourses = new HashSet<StudentCourse>();
    }

    public int StudentID { get; set; }
    public string Name { get; set; }

    public virtual ICollection<StudentCourse> StudentCourses { get; set; }
}

Course

 public partial class Course
{
    public Course()
    {
        this.StudentCourses = new HashSet<StudentCourse>();
    }

    public int CourseID { get; set; }
    public string Title { get; set; }

    public virtual ICollection<StudentCourse> StudentCourses { get; set; }
}

Intermediate model

public partial class StudentCourse
{
    public int StudentCourseID { get; set; }
    public int StudentID { get; set; }
    public int CourseID { get; set; }

    public virtual Course Course { get; set; }
    public virtual Student Student { get; set; }
}

My third question is, do I have to use Virtual key word in above code.

我的第三个问题是,我是否必须在上面的代码中使用虚拟关键字。

1 个解决方案

#1


First, if you do not have an additional field in StudentCourse (such semester of the registration), then you do not need to have StudentCourse class.

首先,如果您在StudentCourse中没有额外的字段(注册的这个学期),那么您不需要拥有StudentCourse课程。

If you want to keep this mode, you can do this:

如果要保留此模式,可以执行以下操作:

StudentCourse registration = new  StudentCourse();
registration.StudentID = 4;
registration.CourseID = 6;
context.StudentCourses.Add(registration);
context.SaveChanges();

These resources may give you further explanation:

这些资源可能会给您进一步解释:

https://practiceaspnet.wordpress.com/2015/10/22/code-first-many-to-many-mm-relationships-using-conventions-and-data-annotations/

https://practiceaspnet.wordpress.com/2015/10/30/managing-data-in-many-to-many-relationships-with-entity-framework-code-first/

If you want to check if there is no duplicate, you can simple do these:

如果要检查是否没有重复,您可以简单地执行以下操作:

 if(context.Courses.Where(c => c.Title == 'Math').FirstOrDefault() == null)
 {
     //add the course
 }else {
     //already existing
 }

#1


First, if you do not have an additional field in StudentCourse (such semester of the registration), then you do not need to have StudentCourse class.

首先,如果您在StudentCourse中没有额外的字段(注册的这个学期),那么您不需要拥有StudentCourse课程。

If you want to keep this mode, you can do this:

如果要保留此模式,可以执行以下操作:

StudentCourse registration = new  StudentCourse();
registration.StudentID = 4;
registration.CourseID = 6;
context.StudentCourses.Add(registration);
context.SaveChanges();

These resources may give you further explanation:

这些资源可能会给您进一步解释:

https://practiceaspnet.wordpress.com/2015/10/22/code-first-many-to-many-mm-relationships-using-conventions-and-data-annotations/

https://practiceaspnet.wordpress.com/2015/10/30/managing-data-in-many-to-many-relationships-with-entity-framework-code-first/

If you want to check if there is no duplicate, you can simple do these:

如果要检查是否没有重复,您可以简单地执行以下操作:

 if(context.Courses.Where(c => c.Title == 'Math').FirstOrDefault() == null)
 {
     //add the course
 }else {
     //already existing
 }