EF Core 5.0

编程入门 行业动态 更新时间:2024-10-21 23:00:56
本文介绍了EF Core 5.0 - 更新 ASP.NET Core Web API 中的多对多实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

With EF Core 5.0 Many-to-many relations are introduced. I'm getting stucked on how to update them through my asp api.

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

For One-to-one and One-to-many relations there is a convention by simply adding the property name followed by 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 请求可能看起来像

So a propper POST Request could look like

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

但我不知道是否有约定或多对多关系的样子

but I could not find out if there is a convention or how it look like for Many-to-many relations

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 请求的正文映射到多对多关系的约定?

Is there a convention to map the body of a http request to Many-to-many relations using EF 5.0?

推荐答案

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

Consider the following two entities which are in many-to-many relationship -

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 列表从客户端,请求有效负载看起来像 -

When updating the Tags in a Post entity, in the most common scenario, a new list of tag Ids are sent from the client-side, and the request payload will look like -

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

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

Typically, you'd want to define a DTO to represent this request object, like -

public class PostUpdateDTO { public int Id { get; set; } public string Title { get; set; } public List<int> Tags { get; set; } }

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

Then, for the update operation itself, you can do something like -

[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 的列表.如果你不喜欢那样想跳过它,你可以试试下面的方法-

As you can see, this requires a trip to the database to fetch a list of all available Tag. If you don't like that and want to skip it, you can try the following approach -

[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 属性类型表示外键.这两个实体都不包含外键属性,因为它们都不依赖于另一个.外键暗示父/子或主体/依赖关系.但是当两个实体处于多对多关系时,它们是相互独立的.

About that - property name followed by ID : The kind of Id property you are referring to represents a foreign-key. Neither of these two entities contains a foreign-key property, because neither of them depends on the other. A foreign-key implies a parent/child or principal/dependent relationship. But when two entities are in many-to-many relation, they are independent of each other.

更多推荐

EF Core 5.0

本文发布于:2023-11-14 14:58:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587645.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:EF   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!