c# - EF Core 5.0 - 更新 ASP.NET Core Web API 中的多对多实体

标签 c# .net entity-framework-core ef-core-5.0

随着 EF Core 5.0 引入了多对多关系。我对如何通过我的 asp .net api 更新它们感到困惑。

对于一对一和一对多关系,有一种约定,只需添加属性名称后跟 ID。

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }

    public BlogImage BlogImage { get; set; }
}

public class BlogImage
{
    public int BlogImageId { get; set; }
    public byte[] Image { get; set; }
    public string Caption { get; set; }

    public int BlogId { get; set; }
    public Blog Blog { get; set; }
}

所以适当的 POST 请求看起来像

{
  "BlogId": 123,
  "Url": "example.com",
  "BlogImageID": 42
}

但我无法确定是否存在约定或多对多关系的样子

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public ICollection<Tag> Tags { get; set; }
}

public class Tag
{
    public string TagId { get; set; }

    public ICollection<Post> Posts { get; set; }
}

是否有使用 EF 5.0 将 http 请求的主体映射到多对多关系的约定?

最佳答案

考虑以下两个处于多对多关系中的实体 -

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

    public ICollection<Tag> Tags { get; set; }
}

public class Tag
{
    public int Id { get; set; }
    public string Name { get; set; }

    public ICollection<Post> Posts { get; set; }
}

Post 实体中更新 Tags 时,在最常见的情况下,一个新的标签 Id 列表会从客户端,请求负载看起来像 -

{
    "id": 123,
    "title": "An Awesome Post",
    "tags": [2, 7, 13]
}

通常,您希望定义一个 DTO 来表示此请求对象,例如 -

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

    public List<int> Tags { get; set; }
}

然后,对于更新操作本身,您可以执行类似 -

[HttpPut]
public async Task Put([FromBody]PostUpdateDTO dto)
{
    // fetch existing Post including related Tags
    var post = await _DbCtx.Posts
        .Include(p => p.Tags)
        .FirstOrDefaultAsync(p => p.Id == dto.Post.Id);

    // remove all Tags from the existing list
    post.Tags.Clear();
    
    // add new Tags to the list whose Ids are sent by the client
    // but to identify them you need the list of all available tags
    var availableTags = await _DbCtx.Tags.ToListAsync();
    foreach (var id in dto.Tags)
    {
        post.Tags.Add(availableTags.First(p => p.Id == id));
    }
    
    // modify properties of Post if you need, like -
    // post.Title = dto.Title;

    await _DbCtx.SaveChangesAsync();
}

如您所见,这需要访问数据库以获取所有可用 Tag 的列表。如果您不喜欢它并想跳过它,您可以尝试以下方法 -

[HttpPut]
public async Task Put([FromBody]PostUpdateDTO dto)
{
    // fetch existing Post including related Tags
    var post = await _DbCtx.Posts
        .Include(p => p.Tags)
        .FirstOrDefaultAsync(p => p.Id == dto.Post.Id);

    // remove Tags which are in the existing Tag list, but not 
    // in the new list sent by the client
    post.Tags.Where(tag => !dto.Tags.Any(id => id == tag.Id))
        .ToList().ForEach(tag => post.Tags.Remove(tag));

    // add Tags which are in the new list sent by the client, but 
    // not in the existing Tag list
    dto.Tags.Where(id => !post.Tags.Any(tag => tag.Id == id))
        .ToList().ForEach(id => post.Tags.Add(new Tag { Id = id }));

    // modify properties of Post if you need, like -
    // post.Title = dto.Title;

    await _DbCtx.SaveChangesAsync();
}

关于 - 属性名称后跟 ID:
您所指的那种 Id 属性代表外键。这两个实体都不包含外键属性,因为它们都不依赖于另一个。外键表示父/子或委托(delegate)人/依赖关系。但是当两个实体处于多对多关系时,它们是相互独立的。

关于c# - EF Core 5.0 - 更新 ASP.NET Core Web API 中的多对多实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65940203/

相关文章:

c# - 尝试使用 .Net 保护 Windows 上的目录

c# - Entity Framework Core 2.1 中的多级模型类

c# - .Net core 3.x Keyless Entity Types 避免表创建

c# - 处理 EntityFramework Core 中的重复键违规

c# - Visual Studio 2015 属性面板

c# - 如何使用正则表达式在 <span> 和 </span> 以及 </span> 和 <span> 之间创建任何内容的数组?

c# - 无法在 C# 应用程序中为我的线程使用多个处理器组

.net - 芯片供应商是否提供任何 .Net JIT 支持?

c# - 如何在 C# 中访问字符串化数组的元素

c# - 顶级目录结构如何揭示应用程序的目的?