以下SchoolContext树是否正确? [英] Is the following tree of SchoolContext correct?

查看:58
本文介绍了以下SchoolContext树是否正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有:

public class Student
{
    public int StudentID { get; set; }
    public string Name { get; set; }
}
public class Teacher
{
    public int TeacherID { get; set; }
    public string Name { get; set; }
}
public SchoolContext : DbContext
{
    public DbSet<Student> Students { get; set; }
    public DbSet<Teacher> Teachers { get; set; }
}

将其理解为跟随树是否正确?

Is it correct to understand it as following tree?

SchoolContext
        |
        |__ DbSet<Student>
        |           |__ An entry1 (includes an Student entity, original values, current values, state of the entity)
        |           |__ An entry2 (includes an Student entity, original values, current values, state of the entity)
        |           |__ ...
        |
        |__ DbSet<Teacher>
                    |__ An entry1 (includes an Teacher entity, original values, current values, state of the entity)
                    |__ An entry2 (includes an Teacher entity, original values, current values, state of the entity)
                    |__ ...

据我所知:


  • 学生实体是Student类的实例。

  • Student entity is an instance of Student class.

原始值是获取数据后该实例的开始值,例如 .Find()方法。

original values are begin values of that instance after we get the data, like .Find() method.

当前值是我们修改后的值。

状态告知实体的当前状态。

state tells the current state of the entity.

推荐答案

如果想要与学生老师之间建立关系,然后处理许多相关的问题。
示例1老师可以有很多学生,而一个学生可以有很多老师,因此这将是多对多的关系。

If u want to have relationship between student teacher, then handle many to many relat. Example 1 teacher can have many student and one student can have many teacher so it will be many to many relationship.

这种情况下会创建另一个班级

That case create another class

class TeacherStudent
{
    public int StudentID { get; set; }
    public Student Student { get; set; }


    public int TeacherID { get; set; }
    public Teacher Teacher { get; set; }
}

public class Student
{
    public int StudentID { get; set; }
    public string Name { get; set; }
    public List<TeacherStudent> TeacherStudents { get; set; }
}
public class Teacher
{
    public int TeacherID { get; set; }
    public string Name { get; set; }
    public List<TeacherStudent> TeacherStudents { get; set; }
}




并在下面的数据库上下文类中覆盖

and in your db context class override below method so TeacherStudent will became a FK relationship.



protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<TeacherStudent>()
            .HasKey(s => new { s.TeacherID, s.StudentID });
    }

这篇关于以下SchoolContext树是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆