本文共 4415 字,大约阅读时间需要 14 分钟。
在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简化了多对多关系的配置,使用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
如需了解更多详细内容,可参考EF Core的官方GitHub仓库。
转载地址:http://xtkkz.baihongyu.com/