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

你可能感兴趣的文章
php实现短信验证功能
查看>>
RabbitMQ连接报错(1)—— None of the specified endpoints were reachable
查看>>
php实现逆转数组
查看>>
PHP实现通过geoip获取IP地理信息
查看>>
PHP实现页面静态化、纯静态化及伪静态化
查看>>
php容许ajax跨域,PHP设置允许ajax跨域请求的两种常见方法
查看>>
RabbitMQ进程结构分析与性能调优
查看>>
PHP对接百度地图
查看>>
PHP对表单提交特殊字符的过滤和处理
查看>>
php对象引用和析构函数的关系
查看>>
RabbitMQ HTTP 认证后端项目常见问题解决方案
查看>>
PHP将图片转换成base64格式(优缺点)
查看>>
php将多个值的数组去除重复元素
查看>>
php局域网上传文件_PHP如何通过CURL上传文件
查看>>
PHP工具插件大全
查看>>
php布尔值的++
查看>>
PHP常量、变量作用域详解(一)
查看>>
PHP应用目录结构设计
查看>>
PHP应用程序连接MSQL数据库Demo(附crud程序)
查看>>
PHP应用程序连接Oracle数据库Demo(附Oracle客户端安装文件)
查看>>