博客
关于我
.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/

你可能感兴趣的文章
python3.6.5+cuda9+cudnn7.1+win10+tensorflow-gpu1.9.0下载配置
查看>>
python3.6使用pickle序列化class
查看>>
python3.6入门到高阶(全栈) day027 网络通信协议 osi七层模型
查看>>
Python3.6属性错误:模块‘;异步没有属性‘;运行‘;
查看>>
python3.6版本中的python-weka-wrapper安装失败,错误代码为%1
查看>>
Python3.7安装cv2踩过的坑
查看>>
python写txt文件
查看>>
python关于sorted排序函数详细解析 附实战代码(全)
查看>>
python3.8调用m文件,调用matlab
查看>>
python3.9新特性_Python 3.9 新特性速览
查看>>
Python3.x:遍历select下拉框获取value值
查看>>
python360百科_11月编程排行榜:Python超过Java成第二
查看>>
Python关于Paginator分页函数详细解析 附实战代码
查看>>
python3、ipython3、setup-tools、pip等环境搭建详细总结
查看>>
python3下使用cv2.imwrite存储带有中文路径图片或者绝对路径图片
查看>>
python3与django中@property详解
查看>>
python3中如何表示无穷大
查看>>
Python3中提示:no module named 'wxpy'
查看>>
python3中选择文件对话框的格式打开和保存图片
查看>>
python3为什么print永远都是地址_Python3 print 函数用法总结
查看>>