asp.net

EntityFrameWorkCore 一对一、一对多和多对多模型的建立

2025-08-24

本文详细介绍在EntityFrameworkCore中如何构建一对一、一对多及多对多的数据模型,包括学生与桌子的一对一关系,学校与老师的关联,以及父母与孩子的多对多联系。


一、一对一模型的建立:

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; }

    public Desk Desk { get; set; }

}

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


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

public  DbSet<Student> Students { get; set; }

public  DbSet<Desk> Desks { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)

     //  Do:一对一关系模型

     modelBuilder.Entity<Student>().HasOne(l => l.Desk).WithOne(l => l.Student).HasForeignKey<Student>(l => l.DeskID); 

}

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


二、一对多的关系模型定义

1、定义模型

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

public class School

{

     public int Id { get; set; }

     public string Name { get; set; }

     public List<Teacher> Teachers { get; set; }

}

public class Teacher

{

     public int Id { get; set; }

     public string Name { get; set; }

 

     public  int SchoolID { get; set; }

     public School School { get; set; }

}


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

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

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

protected override void OnModelCreating(ModelBuilder modelBuilder)

      //  Do:一对多关系模型

      modelBuilder.Entity<Teacher>().HasOne(l => l.School).WithMany(l => l.Teachers).HasForeignKey(l => l.SchoolID); 

}

 

此时通过迁移命令将会生成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中定义两者的关系

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

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

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); 

 

}

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