示例:EntityFrameWorkCore 一对一、一对多和多对多模型的建立

一、目的:测试再EntityFrameWorkCore中如何建立一对一、一对多和多对多模型

 

二、环境:VS2019、.NetCore 2.2、Mysql 8.0、Win10

 

三、一对一模型的建立:

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中定义两者的关系

        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; }
    }
    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中定义两者的关系

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

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

 

Github下载地址:https://github.com/HeBianGu/.NetCore-LearnDemo.git

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值