EFCore 配置多对多关联

在 C# 和 Entity Framework Core (EF Core) 中处理多对多关系非常常见。这种关系类型通常涉及到一个关联表(也称为交叉引用表或连接表),该表用于存储两个实体之间的关联。

例子场景

假设我们有两个实体:Author 和 Book,并且一位作者可以写多本书,一本书也可以有多个作者。为了表示这种关系,我们需要创建一个连接表来存储作者和书籍之间的关系。

步骤 1: 定义实体类

首先定义 Author 和 Book 类:

csharp 复制代码
public class Author
{
    public int Id { get; set; }
    public string Name { get; set; }

    // 导航属性
    public ICollection<Book> Books { get; set; } = new HashSet<Book>();
}

public class Book
{
    public int Id { get; set; }
    public string Title { get; set; }

    // 导航属性
    public ICollection<Author> Authors { get; set; } = new HashSet<Author>();
}

步骤 2: 在 DbContext 中配置关系

在你的 DbContext 类中使用 HasMany().WithMany() 来配置多对多关系:

csharp 复制代码
public class MyDbContext : DbContext
{
    public DbSet<Author> Authors { get; set; }
    public DbSet<Book> Books { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // 配置多对多关系
        modelBuilder.Entity<Author>()
            .HasMany(a => a.Books)
            .WithMany(b => b.Authors)
            .UsingEntity<AuthorBook>(
                ab => ab.HasOne(b => b.Book).WithMany().HasForeignKey(ab => ab.BookId),
                ab => ab.HasOne(a => a.Author).WithMany().HasForeignKey(ab => ab.AuthorId));

        modelBuilder.Entity<AuthorBook>()
            .HasKey(ab => new { ab.AuthorId, ab.BookId });
    }
}

// 这是用于表示多对多关系的连接实体
public class AuthorBook
{
    public int AuthorId { get; set; }
    public Author Author { get; set; }

    public int BookId { get; set; }
    public Book Book { get; set; }
}

步骤 3: 增删改查操作

接下来,我们来具体看下如何执行基本的 CRUD 操作。

插入

插入数据可以通过添加实体到导航属性集合中来实现:

csharp 复制代码
using (var db = new MyDbContext())
{
    var author = new Author { Name = "John Doe" };
    var book = new Book { Title = "Learning EF Core" };

    // 添加双向关联
    author.Books.Add(book);
    book.Authors.Add(author);

    db.Authors.Add(author);
    db.SaveChanges();
}

查询

查询操作可以通过导航属性进行:

csharp 复制代码
using (var db = new MyDbContext())
{
    var author = db.Authors.Include(a => a.Books).FirstOrDefault(a => a.Name == "John Doe");

    // 打印作者的所有书籍标题
    foreach (var book in author.Books)
    {
        Console.WriteLine(book.Title);
    }
}

更新

更新操作通常涉及修改导航属性:

csharp 复制代码
using (var db = new MyDbContext())
{
    var author = db.Authors.Include(a => a.Books).FirstOrDefault(a => a.Name == "John Doe");
    var newBook = new Book { Title = "Advanced EF Core" };

    // 添加新的关联
    author.Books.Add(newBook);
    newBook.Authors.Add(author);

    db.SaveChanges();
}

删除

删除关联可以通过移除导航属性中的元素来完成:

csharp 复制代码
using (var db = new MyDbContext())
{
    var author = db.Authors.Include(a => a.Books).FirstOrDefault(a => a.Name == "John Doe");
    var bookToRemove = author.Books.FirstOrDefault(b => b.Title == "Learning EF Core");

    if (bookToRemove != null)
    {
        // 移除双向关联
        author.Books.Remove(bookToRemove);
        bookToRemove.Authors.Remove(author);

        db.SaveChanges();
    }
}
相关推荐
shepherd枸杞泡茶14 小时前
第3章 3.3日志 .NET Core日志 NLog使用教程
c#·asp.net·.net·.netcore
csdn_aspnet14 小时前
ASP.NET Core 简单文件上传
asp.net·.netcore
亦世凡华、2 天前
掌握.NET Core后端发布流程,如何部署后端应用?
经验分享·.netcore·docker部署·程序发布
contact972 天前
.NET Core中的五种过滤器详解
.netcore·过滤器
以为不会掉头发的詹同学2 天前
【 Avalonia UI 语言国际化 I18n】图文结合教学,保姆级教学,语言国际化就是这么简单(.Net C#)
开发语言·前端·c#·.netcore·用户界面
爱吃香蕉的阿豪5 天前
在c#中虚方法和抽象类的区别
深度学习·c#·.netcore
shepherd枸杞泡茶5 天前
第3章 .NETCore核心基础组件:3.1 .NET Core依赖注入
开发语言·c#·.net·.netcore
.NET快速开发框架5 天前
使用nvm管理node.js版本,方便vue2,vue3开发
vue·.netcore·常用工具·开发技术·rdif
csdn_aspnet7 天前
ASP.NET Core 使用 FileStream 将 FileResult 文件发送到浏览器后删除该文件
asp.net·.netcore
csdn_aspnet7 天前
ASP.NET Core SixLabors.ImageSharp v1.0 的图像实用程序类 web示例
asp.net·.netcore