如何在多对多模型中添加成分列表?

时间:2019-11-25 15:38:58

标签: c# entity-framework asp.net-core many-to-many

我有供应商和成分模型,他们可以互相看到。 我有一份配料表,我需要以某种方式将其添加到供应商。 无法理解该怎么做。

    public void AddIngredientsToVendor(VendorIngredientsRegistrationModel model)
    {
        // Ingredients, that I want to add to wendor, Ids: [57, 59, 61, 62]
        List<Ingredient> ingredients = _repository.Fetch<Ingredient>(e => model.IngredientIds.Contains(e.Id),
                   e => e.Include(p => p.VendorIngredients)).ToList();

        var vendor = _repository.First<Vendor>(
            (e => e.Id == model.VendorId),
             e => e.Include(m => m.Ingredients));

        // How to add ingredients to Vendor?

        _repository.Update(vendor);
        _repository.SaveChanges();
    }

VendorIngredient.cs

public class VendorIngredient
{
    [ForeignKey(nameof(VendorId))]
    public Vendor Vendor { get; set; }
    public long VendorId { get; set; }

    [ForeignKey(nameof(IngredientId))]
    public Ingredient Ingredient { get; set; }
    public long IngredientId { get; set; }
}

Vendor.cs

public class Vendor : BaseNetworkLinkedEntity
{
    public Vendor()
    {
        Ingredients = new List<VendorIngredient>();
    }

    [MaxLength(150)]
    public string VendorCompanyName { get; set; }
    public ICollection<VendorIngredient> Ingredients { get; set; }
}

2 个答案:

答案 0 :(得分:0)

假设供应商在AddIngredientsToVendor方法中处于上下文中,如果您愿意将供应商上的集合类型更改为Ingreedient,则它应该与以下内容一样简单。供应商成分的概念似乎在这里失去了价值,因为它所做的只是将供应商名称附加到供应商合集中的每个组件上

public void AddIngredientsToVendor(VendorIngredientsRegistrationModel model)
{
    // Ingredients, that I want to add to wendor, Ids: [57, 59, 61, 62]
    List<Ingredient> ingredients = _repository.Fetch<Ingredient>(e => model.IngredientIds.Contains(e.Id),
               e => e.Include(p => p.VendorIngredients)).ToList();

    vendor.Ingredients = ingredients;

    _repository.Update(vendor);
    _repository.SaveChanges();
}

如果您想将其保留为VendorIngredient,则需要编写一种方法,该方法同时包含成分和供应商,然后将其映射到VendorIngredient

答案 1 :(得分:0)

由于新版本的EF核心不再提供以下模式:Saving many-to-many relationship in Entity Framework Core,因此您必须根据自己的逻辑来管理bridge-table

我能想到的最好的例子是:UserManager.AddToRolesAsync from ASP.NET Core IdentityFramework

您可以从上面的链接中找到这2种方法:AddToRolesAsyncRemoveFromRolesAsync为例。

所以在您的情况下,

public void AddIngredientsToVendor(VendorIngredientsRegistrationModel model)
{
    // Ingredients, that I want to add to wendor, Ids: [57, 59, 61, 62]
    List<Ingredient> ingredients = _repository.Fetch<Ingredient>(e => model.IngredientIds.Contains(e.Id),
               e => e.Include(p => p.VendorIngredients)).ToList();

    var vendor = _repository.First<Vendor>(
        (e => e.Id == model.VendorId),
         e => e.Include(m => m.Ingredients));

    // How to add ingredients to Vendor?
   ingredients.ForEach(ingredient =>
   {
        if(vendor.Ingredients.Any(x => x.IngredientId != ingredient.Id))
        {
            vendor.Ingredients.Add(new VendorIngredient
            {
                VendorId = vendor.Id,
                IngredientId = ingredient.Id
            });
        }
   }
    _repository.Update(vendor);
    _repository.SaveChanges();
}