Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var post = await dbContext.Posts
- .SingleOrDefaultAsync(someId);
- post.PostCategories = ... Some new collection...; // <<<
- dbContext.Posts.Update(post);
- await dbContext.SaveChangesAsync();
- public static class EFCoreExtensions
- {
- public static void UpdateLinks<TLink, TFromId, TToId>(this DbSet<TLink> dbSet,
- Expression<Func<TLink, TFromId>> fromIdProperty, TFromId fromId,
- Expression<Func<TLink, TToId>> toIdProperty, IEnumerable<TToId> toIds)
- where TLink : class, new()
- {
- // link => link.FromId == fromId
- Expression<Func<TFromId>> fromIdVar = () => fromId;
- var filter = Expression.Lambda<Func<TLink, bool>>(
- Expression.Equal(fromIdProperty.Body, fromIdVar.Body),
- fromIdProperty.Parameters);
- var existingLinks = dbSet.AsTracking().Where(filter);
- var toIdSet = new HashSet<TToId>(toIds);
- if (toIdSet.Count == 0)
- {
- //The new set is empty - delete all existing links
- dbSet.RemoveRange(existingLinks);
- return;
- }
- // Delete the existing links which do not exist in the new set
- var toIdSelector = toIdProperty.Compile();
- foreach (var existingLink in existingLinks)
- {
- if (!toIdSet.Remove(toIdSelector(existingLink)))
- dbSet.Remove(existingLink);
- }
- // Create new links for the remaining items in the new set
- if (toIdSet.Count == 0) return;
- // toId => new TLink { FromId = fromId, ToId = toId }
- var toIdParam = Expression.Parameter(typeof(TToId), "toId");
- var createLink = Expression.Lambda<Func<TToId, TLink>>(
- Expression.MemberInit(
- Expression.New(typeof(TLink)),
- Expression.Bind(((MemberExpression)fromIdProperty.Body).Member, fromIdVar.Body),
- Expression.Bind(((MemberExpression)toIdProperty.Body).Member, toIdParam)),
- toIdParam);
- dbSet.AddRange(toIdSet.Select(createLink.Compile()));
- }
- }
- var post = await dbContext.Posts
- .SingleOrDefaultAsync(someId);
- dbContext.Set<PostCategory>().UpdateLinks(pc =>
- pc.PostId, post.Id, pc => pc.CategoryId, postCategories.Select(pc => pc.CategoryId));
- await dbContext.SaveChangesAsync();
- dbContext.Set<PostCategory>().UpdateLinks(pc =>
- pc.PostId, post.Id, pc => pc.CategoryId, postCategoryIds);
- dbContext.Set<PostCategory>().UpdateLinks(pc =>
- pc.PostId, post.Id, pc => pc.CategoryId, postCategories.Select(c => c.Id));
Add Comment
Please, Sign In to add comment