博客
关于我
.NET 云原生架构师训练营(模块二 基础巩固 EF Core 关系)--学习笔记
阅读量:421 次
发布时间:2019-03-06

本文共 5374 字,大约阅读时间需要 17 分钟。

2.4.4 EF Core -- 关系

  • 一对多
  • 一对一
  • 多对多
  • 示例

关系:

一对多

// Dependent Entity 主表public class Blog{    // Principal Key 标识键/可能是主键或者备用键(唯一性约束)    public int BlogId { get; set; }        public string Url { get; set; }    // Collection navigation property 关联多个从表的属性集合(集合属性)    public List
Posts { get; set; }}// Principal Entity 从表public class Post{ public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; } // Foreign Key 外键(指向主表中的 Principal Key) // Inverse navigation property 反向导航属性 public int BlogId { get; set; } // Inverse navigation property 反向导航属性 public Blog Blog { get; set; }}

一对一

// Principal Entity 从表public class Blog{    public int BlogId { get; set; }        public string Url { get; set; }    public List
Posts { get; set; }}// Dependent Entity 主表public class Post{ public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; } // Reference navigation property 一对一时指向另外一张表(引用属性) public Blog Blog { get; set; }}

多对多

public class Post{    public int PostId { get; set; }        public string Title { get; set; }        public string Content { get; set; }    public ICollection
Tags { get; set; }}public class Tag{ public string TagId { get; set; } public ICollection
Posts { get; set; }}

示例

一对多

一个 Project 对应多个 ProjectGroup

在 Project 实体中添加 ProjectGroup 列表

public List
Groups { get; set; }

迁移

dotnet ef migrations add ProjectGroupCollectionProperty

生成集合属性 ProjectGroupCollectionProperty

protected override void Up(MigrationBuilder migrationBuilder){    migrationBuilder.AlterColumn
( name: "ProjectId", table: "ProjectGroups", nullable: true, oldClrType: typeof(string), oldType: "longtext CHARACTER SET utf8mb4", oldNullable: true); migrationBuilder.CreateIndex( name: "IX_ProjectGroups_ProjectId", table: "ProjectGroups", column: "ProjectId"); migrationBuilder.AddForeignKey( name: "FK_ProjectGroups_Projects_ProjectId", table: "ProjectGroups", column: "ProjectId", principalTable: "Projects", principalColumn: "Id", onDelete: ReferentialAction.Restrict);}

手动配置

class MyContext : DbContext{    public DbSet
Blogs { get; set; } public DbSet
Posts { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { // 先在从表上建立一对一的关系,再从主表上建立一对多的关系 modelBuilder.Entity
() .HasOne(p => p.Blog) .WithMany(b => b.Posts); }}public class Blog{ public int BlogId { get; set; } public string Url { get; set; } public List
Posts { get; set; }}public class Post{ public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; } public Blog Blog { get; set; }}

LighterDbContext

// 一对一modelBuilder.Entity
().HasOne
(g => g.Project);// 一对多modelBuilder.Entity
().HasOne
(g => g.Project).WithMany(p => p.Groups);

多对多

为 Project 和 Subject 建立中间表 SubjectProject

public class Project : Entity{    public string Title { get; set; }    public DateTime StartDate { get; set; }    public DateTime EndDate { get; set; }    public string SupervisorId { get; set; }    public string PlanId { get; set; }    public List
Groups { get; set; } public List
SubjectProjects { get; set; }}public class Subject : Entity{ public string Title { get; set; } public string Content { get; set; } public List
SubjectProjects { get; set; }}public class SubjectProject : Entity{ public string ProjcetId { get; set; } public Project Project { get; set; } public string SubjectId { get; set; } public Subject Subject { get; set; }}

配置多对多关系

LighterDbContext

// 多对多(两组一对多)modelBuilder.Entity
() .HasOne
(s => s.Project) .WithMany(p => p.SubjectProjects) .HasForeignKey(s => s.ProjcetId); modelBuilder.Entity
() .HasOne
(s => s.Subject) .WithMany(p => p.SubjectProjects) .HasForeignKey(s => s.SubjectId);

迁移

dotnet ef migrations add SubjectProjectManyToManyRelation

SubjectProjectManyToManyRelation

table.ForeignKey(    name: "FK_SubjectProject_Projects_ProjcetId",    column: x => x.ProjcetId,    principalTable: "Projects",    principalColumn: "Id",    onDelete: ReferentialAction.Restrict);table.ForeignKey(    name: "FK_SubjectProject_Subject_SubjectId",    column: x => x.SubjectId,    principalTable: "Subject",    principalColumn: "Id",    onDelete: ReferentialAction.Restrict);

中间表创建了两个外键,形成多对多

EF Core 5.0 多对多实现

public class Post{    public int PostId { get; set; }    public string Title { get; set; }    public string Content { get; set; }    public ICollection
Tags { get; set; }}public class Tag{ public string TagId { get; set; } public ICollection
Posts { get; set; }}

迁移的时候会自动生成中间表

联接实体类型配置 HasMany

modelBuilder    .Entity
() .HasMany(p => p.Tags) .WithMany(p => p.Posts) .UsingEntity(j => j.ToTable("PostTags"));

GitHub源码链接:

本作品采用进行许可。

欢迎转载、使用、重新发布,但务必保留文章署名 郑子铭 (包含链接: ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。

如有任何疑问,请与我联系 (MingsonZheng@outlook.com) 。

你可能感兴趣的文章
MySQL 基础模块的面试题总结
查看>>
MySQL 备份 Xtrabackup
查看>>
mysql 多个表关联查询查询时间长的问题
查看>>
mySQL 多个表求多个count
查看>>
mysql 多字段删除重复数据,保留最小id数据
查看>>
MySQL 多表联合查询:UNION 和 JOIN 分析
查看>>
MySQL 大数据量快速插入方法和语句优化
查看>>
mysql 如何给SQL添加索引
查看>>
mysql 字段区分大小写
查看>>
mysql 字段合并问题(group_concat)
查看>>
mysql 字段类型类型
查看>>
MySQL 字符串截取函数,字段截取,字符串截取
查看>>
MySQL 存储引擎
查看>>
mysql 存储过程 注入_mysql 视图 事务 存储过程 SQL注入
查看>>
MySQL 存储过程参数:in、out、inout
查看>>
mysql 存储过程每隔一段时间执行一次
查看>>
mysql 存在update不存在insert
查看>>
Mysql 学习总结(86)—— Mysql 的 JSON 数据类型正确使用姿势
查看>>
Mysql 学习总结(87)—— Mysql 执行计划(Explain)再总结
查看>>
Mysql 学习总结(88)—— Mysql 官方为什么不推荐用雪花 id 和 uuid 做 MySQL 主键
查看>>