EF Core一对多和多对多

 

1.Model配置

Entity Framework Core 提供了两种方式

A.使用 fluent API 配置模型

可在派生上下文中覆写 OnModelCreating 方法,并使用 ModelBuilder API 来配置模型。 此配置方法最为有效,并可在不修改实体类的情况下指定配置。 Fluent API 配置具有最高优先级,并将替代约定和数据注释。

    class MyContext : DbContext
    {
        public DbSet Blogs { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity()
                .Property(b => b.Url)
                .IsRequired();
        }
    }

B.使用数据注释来配置模型

也可将特性(称为数据注释)应用于类和属性。 数据注释会替代约定,但会被 Fluent API 配置替代。

    public class Blog
    {
        public int BlogId { get; set; }
        [Required]
        public string Url { get; set; }
    }

一对多

以city和Province为例,Province有多个city,而一个city只能有一个Province

 

using System;
using Microsoft.EntityFrameworkCore;

namespace ASPNetEFFCore.Models
{
    public class MyContext:DbContext
    {
        public MyContext(DbContextOptions options):base(options)
        {
        }

       protected override void OnModelCreating(ModelBuilder modelBuilder){

            //配置多对多 ,就是两个一对多,可以不写

            modelBuilder.Entity().HasOne(city => city.Province).WithMany(x => x.Cities).HasForeignKey(city => .ProviceId);
        }

        public DbSet Provinces { get; set; }
        public DbSet Cities { get; set; }


    }
    public class Province
    {
        public Province()
        {
            Cities = new List();
        }

        public int Id { get; set; }
        public string Name { get; set; }
        public int Population { get; set; }
        public List Cities { get; set; }
    }

     public class City
    {
     

        public int Id { get; set; }
        public string Name { get; set; }
        public string AreaCode { get; set; }
        public int ProviceId { get; set; }
        public Province Province { get; set; }

         public Mayor Mayor {get;set;}
    }
}

多对多

以city和company为例,一个city会有多个company,一个company在多个city都有多个分company

using System;
using Microsoft.EntityFrameworkCore;

namespace ASPNetEFFCore.Models
{
    public class MyContext:DbContext
    {
        public MyContext(DbContextOptions options):base(options)
        {
        }

       protected override void OnModelCreating(ModelBuilder modelBuilder){
           //使用x.CityId,x.CompanyId生成 cityCompany的联合主键
           //执行数据库迁移
           modelBuilder.Entity().HasKey(x => new{x.CityId,x.CompanyId});

            //配置多对多 ,就是两个一对多,可以不写
           modelBuilder.Entity().HasOne(x => x.City).WithMany(x=> x.CityCompany).HasForeignKey(x=>x.CityId);
         modelBuilder.Entity().HasOne(x => x.Company).WithMany(x=> x.CityCompany).HasForeignKey(x=>x.CompanyId);
    
        }
  
        public DbSet Cities { get; set; }

        public DbSet cityCompanies {get;set;}
    }
    
    public class City
    {
         public City()
        {
            CityCompany = new List();
        }

        public int Id { get; set; }
        public string Name { get; set; }
        public string AreaCode { get; set; }
        public int ProvinceId { get; set; }
        public Province Province { get; set; }

        //多对多映射
        public List CityCompany {get;set;}
    }

     public class Company
    {
        public Company()
        {
            CityCompany = new List();
        }

        public int Id {get;set;}
        public string Name {get;set;}
        public DateTime EstablishDate {get;set;}
        public string LegalPerson {get;set;}

        //多对多映射
        public List CityCompany {get;set;}
    }
    //多对多中间model
    public class CityCompany
    {
       public int CityId {get;set;}
       public City City{get;set;}
       public int CompanyId {get;set;} 
       public Company Company {get;set;}
    }
}

最后执行迁移命令

 

你可能感兴趣的:(Entity,FrameWork,core)