博客
关于我
.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中,可以通过HasOne和WithMany方法来配置一对多关系:

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中,可以通过HasOne和WithOne方法来配置一对一关系:

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中,可以通过HasMany和WithMany方法来配置多对多关系:

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简化了多对多关系的配置,使用HasMany和WithMany方法:

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/

你可能感兴趣的文章
Python中实现ASCII码与字符相互转换
查看>>
Python中实现获取所有微信好友的头像并拼接成一张图片
查看>>
Python中对象的布尔值
查看>>
python中对集合的操作
查看>>
python中将列表转换为元组的时间复杂度,反之亦然
查看>>
Python中布尔表达式的求值
查看>>
python中很常用的10个内置函数整理(初学必备)
查看>>
python中打印二维列表数据
查看>>
python中打散数组shuffle
查看>>
Python中执行外部命令
查看>>
Python中拼接numpy数组-ChatGPT4o作答
查看>>
Python中提示:no module named 'PIL'
查看>>
Python中提示:UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position
查看>>
python中数据类型存储_浅谈Python中的数据类型
查看>>
python中文件最重要的功能是什么-学 Python 都用来干嘛的?
查看>>
python中文版语言_高盛最新调查:Python超过汉语成为未来最重要技能,你准备学哪种编程语言?...
查看>>
Python中文问题SyntaxError
查看>>
Python中方括号和括号括起来的列表有什么区别?
查看>>
python中时间处理_Python中处理时间的几种方法小结
查看>>
Python中枚举串口(包括虚拟端口)的跨平台方法是什么?
查看>>