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

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

EF Core关系

在EF Core中,关系是定义 entity 之间的关联方式,常见的关系类型包括一对多、一对一和多对多。本文将详细介绍这些关系类型及其配置方法。


一对多

一对多关系表示一个主表中的单个实体可以关联到多个从表实体。以下是典型的代码示例:

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

配置方法

在EF Core中,可以通过HasOneWithMany方法来配置一对多关系:

using Microsoft.EntityFrameworkCore;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); }}

迁移步骤

使用dotnet ef migrations生成迁移:

dotnet ef migrations add BlogPostCollectionProperty

一对一

一对一关系表示一个主表中的单个实体可以关联到另一个单独的从表实体。以下是典型的代码示例:

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

配置方法

在EF Core中,可以通过HasOneWithOne方法来配置一对一关系:

using Microsoft.EntityFrameworkCore;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) .WithOne(post => post.Blog); }}

迁移步骤

使用dotnet ef migrations生成迁移:

dotnet ef migrations add BlogSingleRelationship

多对多

多对多关系表示两个独立的实体可以通过中间表关联到多个实体。以下是典型的代码示例:

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; }}public class PostTag{ public int PostId { get; set; } public Post Post { get; set; } public string TagId { get; set; } public Tag Tag { get; set; }}

配置方法

在EF Core中,可以通过HasManyWithMany方法来配置多对多关系:

using Microsoft.EntityFrameworkCore;class MyContext : DbContext{    public DbSet
Posts { get; set; } public DbSet
Tags { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { // 多对多关系配置 modelBuilder.Entity
() .HasOne(pt => pt.Post) .WithMany(p => p.Tags) .HasForeignKey(pt => pt.PostId); modelBuilder.Entity
() .HasOne(pt => pt.Tag) .WithMany(t => t.Posts) .HasForeignKey(pt => pt.TagId); }}

迁移步骤

使用dotnet ef migrations生成迁移:

dotnet ef migrations add PostTagManyToManyRelation

示例:项目与项目组

以下是一个实际项目中常见的案例:

一对多示例

public class Project{    public int Id { get; set; }    public string Name { get; set; }    public int? UserId { get; set; }    public List
Groups { get; set; }}public class ProjectGroup{ public int Id { get; set; } public string Name { get; set; } public int ProjectId { get; set; }}

配置方法

MyContext中添加关系配置:

using Microsoft.EntityFrameworkCore;class MyContext : DbContext{    public DbSet
Projects { get; set; } public DbSet
ProjectGroups { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { // 一对多关系配置 modelBuilder.Entity
() .HasOne(p => p.Project) .WithMany(g => g.Groups); }}

迁移步骤

运行以下命令生成迁移:

dotnet ef migrations add ProjectGroupCollectionProperty

EF Core 5.0 多对多实现

EF Core 5.0简化了多对多关系的配置,使用HasManyWithMany方法:

using Microsoft.EntityFrameworkCore;class MyContext : DbContext{    public DbSet
Posts { get; set; } public DbSet
Tags { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { // 多对多关系配置 modelBuilder.Entity
() .HasMany(p => p.Tags) .WithMany(t => t.Posts) .UsingEntity(e => e.ToTable("PostTags")); }}

迁移步骤

运行以下命令生成迁移:

dotnet ef migrations add PostTagCollection

GitHub源码链接

如需了解更多详细内容,可参考EF Core的官方GitHub仓库

转载地址:http://xtkkz.baihongyu.com/

你可能感兴趣的文章
Objective-C实现RRT路径搜索(附完整源码)
查看>>
Objective-C实现RS485通信接收数据(附完整源码)
查看>>
Objective-C实现rsa 密钥生成器算法(附完整源码)
查看>>
Objective-C实现RSA密码算法(附完整源码)
查看>>
Objective-C实现RSA素因子算法(附完整源码)
查看>>
Objective-C实现runge kutta龙格-库塔法算法(附完整源码)
查看>>
Objective-C实现Sarsa算法(附完整源码)
查看>>
Objective-C实现SCC的Kosaraju算法(附完整源码)
查看>>
Objective-C实现scoring functions评分函数算法(附完整源码)
查看>>
Objective-C实现scoring评分算法(附完整源码)
查看>>
Objective-C实现searching in sorted matrix在排序矩阵中搜索算法(附完整源码)
查看>>
Objective-C实现Secant method割线法算法(附完整源码)
查看>>
Objective-C实现segment tree段树算法(附完整源码)
查看>>
Objective-C实现segmented sieve分段筛算法(附完整源码)
查看>>
Objective-C实现selection sort选择排序算法(附完整源码)
查看>>
Objective-C实现sha1算法(附完整源码)
查看>>
Objective-C实现sha256算法(附完整源码)
查看>>
Objective-C实现shell sort希尔排序算法(附完整源码)
查看>>
Objective-C实现sherman morrison公式算法(附完整源码)
查看>>
Objective-C实现ShorAlgorithm肖尔算法 (附完整源码)
查看>>