EF Core 一对一 一对多 多对多 关系定义

EF Core 一对一 一对多 多对多 关系定义

1、定义模型

示例:学生和桌子的一对一关系:每个学生需要对应一个桌位信息,桌位信息不用包含学生信息

public class Desk
{
  public int Id { get; set; }
  public string Name { get; set; }
  public Student Student { get; set; }
}
//这两个就是无论在那个就是就是在对应的一个添加上对应的类的主键
public class Student
{
  public int Id { get; set; }
  public string Name { get; set; }
  public int DeskID { get; set; }//这个是对应Desk的主键,是Student的外键
  public Desk Desk { get; set; }
}

在Student中定义 DeskID和Desk模型,在Desk表中定义Student模型

2、在DataContext中定义两者的关系

protected override void OnModelCreating(ModelBuilder modelBuilder)
{ 

  // Do:一对一关系模型
  modelBuilder.Entity<Student>().HasOne(l => l.Desk).WithOne(l => l.Student)
  .HasForeignKey<Student>(l => l.DeskID); 
}
public DbSet<Student> Students { get; set; }
public DbSet<Desk> Desks { get; set; }

此时通过迁移命令将会生成Students表和Desks表

1、定义模型

示例:学校和老师的一对多关系:一个学校对应多个老师,一个老师对应一个学校

public class School
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Teacher> Teachers { get; set; }//在"——"中,添加List<多个>
}
public class Teacher
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int SchoolID { get; set; }//这个SchoolID是Teacher的外键,对应School的逐渐;系统会自己配置的
    public School School { get; set; }//在"多"中,添加"一"对应的类的名字和ID
}

2、在DataContext中定义两者的关系

protected override void OnModelCreating(ModelBuilder modelBuilder)
{ 

  // Do:一对多关系模型
  modelBuilder.Entity<Teacher>().HasOne(l => l.School).WithMany(l => l.Teachers)
    .HasForeignKey(l => l.SchoolID); 
}


public DbSet<Teacher> Teachers { get; set; }
public DbSet<School> Schools { get; set; }

此时通过迁移命令将会生成Schools表和Teachers表

五、多对多的关系模型

   多对多的模型,就是需要创建第三个类,来关联两个多多类
1、定义模型:

示例:建立父母和孩子的多对多模型,父母可以对应多个孩子,孩子可以有父亲,母亲的对应关系

// Do:定义父母类型
public class Parent
{
  public Parent()
  {
    this.RelationShips =new List<RelationShip>();
  }
  public int Id { get; set; }

  public string Name { get; set; }

  // Do:3、定义关系集合
  public List<RelationShip> RelationShips { get; set; }
}
// Do:定义子类型
public class Child
{
  public Child()
  {
    this.RelationShips=new List<RelationShip>();
  }
  public int Id { get; set; }

  public string Name { get; set; }

  // Do:2、定义关系集合
  public List<RelationShip> RelationShips { get; set; }
}
 
/// <summary>
/// 1、多对多关系模型
/// </summary>
public class RelationShip
{
  public int ChildID { get; set; }

  public Child Child { get; set; }

  public int ParentID { get; set; }

  public Parent Parent { get; set; }
}

2、在DataContext中定义两者的关系

protected override void OnModelCreating(ModelBuilder modelBuilder)
{ 

  // Do:多对多配置联合主键 
  modelBuilder.Entity<RelationShip>().HasKey(l => new {l.ChildID, l.ParentID});

  // Do:多对多定义关系模型映射(本段代码可有可无)
  modelBuilder.Entity<RelationShip>().HasOne(l => l.Child).WithMany(l => l.RelationShips)
    .HasForeignKey(l => l.ChildID);

  modelBuilder.Entity<RelationShip>().HasOne(l => l.Parent).WithMany(l => l.RelationShips)
    .HasForeignKey(l => l.ParentID); 

}

  public DbSet<Teacher> Teachers { get; set; }
  public DbSet<School> Schools { get; set; }
原文地址:https://www.cnblogs.com/ZkbFighting/p/13129661.html