Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

c# - EF Core 5.0 - Updating many-to-many entities in ASP.NET Core Web API

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

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; }
}

So a propper POST Request could look like

{
  "BlogId": 123,
  "Url": "example.com",
  "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; }
}

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

question from:https://stackoverflow.com/questions/65940203/ef-core-5-0-updating-many-to-many-entities-in-asp-net-core-web-api

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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; }
}

When updating the Tags in a Post entity, you need to pass a new list of Id for the Tag entities from the client-side.

Typically, you'd want to define a DTO like -

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

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

For the updating 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 tags in the database first
    var tags = await _DbCtx.Tags.ToListAsync();
    foreach (var id in dto.Tags)
    {
        post.Tags.Add(tags.First(p => p.Id == id));
    }
    
    // modify properties of Post if you need, like -
    // post.Title = dto.Title;

    await _DbCtx.SaveChangesAsync();
}

As you can see, this requires a trip to the database to fetch a list of all existing Tag. If you don't like that, 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();
}

About the Id :
Neither of these two entities contains the kind of Id (a foreign-key) you are referring to, 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...