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, 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]
}
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();
}
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();
}
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.