Обновление ядра Entity framework многие ко многим

мы портируем наше существующее приложение MVC6 EF6 на core.

есть ли простой метод в EF core для обновления отношения "многие ко многим"?

мой старый код из EF6, где мы очищаем список и перезаписываем его новыми данными, больше не работает.

           var model = await _db.Products.FindAsync(vm.Product.ProductId);

            model.Colors.Clear();

            model.Colors =  _db.Colors.Where(x => 
            vm.ColorsSelected.Contains(x.ColorId)).ToList();

2 ответов


это будет работать для вас.

сделайте класс, чтобы иметь отношение в

public class ColorProduct
{
    public int ProductId { get; set; }
    public int ColorId { get; set; }

    public Color Color { get; set; }
    public Product Product { get; set; }
}

добавьте коллекцию ColorProduct в свой продукт и цветовые классы

 public ICollection<ColorProduct> ColorProducts { get; set; }

затем используйте это расширение, которое я сделал, чтобы удалить невыбранные и добавить вновь выбранные в список

    public static void TryUpdateManyToMany<T, TKey>(this DbContext db, IEnumerable<T> currentItems, IEnumerable<T> newItems, Func<T, TKey> getKey) where T : class
    {
        db.Set<T>().RemoveRange(currentItems.Except(newItems, getKey));
        db.Set<T>().AddRange(newItems.Except(currentItems, getKey));
    }

    public static IEnumerable<T> Except<T, TKey>(this IEnumerable<T> items, IEnumerable<T> other, Func<T, TKey> getKeyFunc)
    {
        return items
            .GroupJoin(other, getKeyFunc, getKeyFunc, (item, tempItems) => new { item, tempItems })
            .SelectMany(t => t.tempItems.DefaultIfEmpty(), (t, temp) => new { t, temp })
            .Where(t => ReferenceEquals(null, t.temp) || t.temp.Equals(default(T)))
            .Select(t => t.t.item);
    }

использование выглядит так

 var model = _db.Products
             .Include(x => x.ColorProducts)
             .FirstOrDefault(x => x.ProductId == vm.Product.ProductId);

 _db.TryUpdateManyToMany(model.ColorProducts, vm.ColorsSelected
 .Select(x => new ColorProduct
 {
     ColorId = x,
     ProductId = vm.Product.ProductId
 }), x => x.ColorId);

чтобы избежать ада LINQ в приведенном выше ответе, шаблонный метод "кроме" может быть переписан как таковой:

public static IEnumerable<TEntity> LeftComplementRight<TEntity, TKey>(
        this IEnumerable<TEntity> left,
        IEnumerable<TEntity> right,
        Func<TEntity, TKey> keyRetrievalFunction)
    {
        var leftSet = left.ToList();
        var rightSet = right.ToList();

        var leftSetKeys = leftSet.Select(keyRetrievalFunction);
        var rightSetKeys = rightSet.Select(keyRetrievalFunction);

        var deltaKeys = leftSetKeys.Except(rightSetKeys);
        var leftComplementRightSet = leftSet.Where(i => deltaKeys.Contains(keyRetrievalFunction.Invoke(i)));
        return leftComplementRightSet;
    }

кроме того, метод UpdateManyToMany может быть обновлен для включения объектов, которые были изменены как таковые:

public void UpdateManyToMany<TDependentEntity, TKey>(
        IEnumerable<TDependentEntity> dbEntries,
        IEnumerable<TDependentEntity> updatedEntries,
        Func<TDependentEntity, TKey> keyRetrievalFunction)
        where TDependentEntity : class
    {
        var oldItems = dbEntries.ToList();
        var newItems = updatedEntries.ToList();
        var toBeRemoved = oldItems.LeftComplementRight(newItems, keyRetrievalFunction);
        var toBeAdded = newItems.LeftComplementRight(oldItems, keyRetrievalFunction);
        var toBeUpdated = oldItems.Intersect(newItems, keyRetrievalFunction);

        this.Context.Set<TDependentEntity>().RemoveRange(toBeRemoved);
        this.Context.Set<TDependentEntity>().AddRange(toBeAdded);
        foreach (var entity in toBeUpdated)
        {
            var changed = newItems.Single(i => keyRetrievalFunction.Invoke(i).Equals(keyRetrievalFunction.Invoke(entity)));
            this.Context.Entry(entity).CurrentValues.SetValues(changed);
        }
    }

который использует другую пользовательскую шаблонную функцию "Intersect", чтобы найти пересечение двух наборов:

public static IEnumerable<TEntity> Intersect<TEntity, TKey>(
        this IEnumerable<TEntity> left,
        IEnumerable<TEntity> right,
        Func<TEntity, TKey> keyRetrievalFunction)
    {
        var leftSet = left.ToList();
        var rightSet = right.ToList();

        var leftSetKeys = leftSet.Select(keyRetrievalFunction);
        var rightSetKeys = rightSet.Select(keyRetrievalFunction);

        var intersectKeys = leftSetKeys.Intersect(rightSetKeys);
        var intersectionEntities = leftSet.Where(i => intersectKeys.Contains(keyRetrievalFunction.Invoke(i)));
        return intersectionEntities;
    }