示例: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().HasOne(l => l.Desk).WithOne(l => l.Student)
                .HasForeignKey(l => l.DeskID); 

        }

        public  DbSet Students { get; set; }
        public DbSet Desks { get; set; }

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

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

1、定义模型

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

    public class School
   {
       public int Id { get; set; }
       public string Name { get; set; }
       public List 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().HasOne(l => l.School).WithMany(l => l.Teachers)
                .HasForeignKey(l => l.SchoolID); 
        }


        public  DbSet Teachers { get; set; }
        public DbSet Schools { get; set; }

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

五、多对多的关系模型

1、定义模型:

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

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

        public string Name { get; set; }

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

        public string Name { get; set; }

        //  Do:2、定义关系集合
        public List RelationShips { get; set; }
    }

 

    /// 
    /// 1、多对多关系模型
    /// 
    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().HasKey(l => new {l.ChildID, l.ParentID});

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

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

        }

        public DbSet Teachers { get; set; }
        public DbSet Schools { get; set; }

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

 

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

你可能感兴趣的:(.Net,Core,C#)