0

I have models for Vendor and Ingredient, they can see each other. I have a list of ingredients and I need to add it somehow to vendor. Can't understand how to do it.

    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 Answers2

0

Assuming that vendor is in context in the AddIngredientsToVendor method, it should be as simple as the following if you are willing to change the type of the collection on the vendor to be just Ingreedient. The concept of a vendor ingredient seems to lose value here as all it does is attach the vendor name to each ingrediet in the colelction of a vendor

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

If you were to want to keep it as VendorIngredient you would need to write a method that would take in both an Ingredient and a Vendor and then map that down to the VendorIngredient class

David Watts
  • 2,249
  • 22
  • 33
  • Sorry, I added a definition for vendor. With `vendor.Ingredients = ingredients;` it says: "Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.ICollection'" – Dmytro Kozyr Nov 25 '19 at 15:50
  • Ah, your models are of different types, you will need to write a method to convert `Ingredient` To `VendorIngredient`. The value of a vendor ingredient is questionable here in your vendor class though as why does each ingredient within the vendor need the vendor name against it? I would change the type of the Ingredients Collection on the vendor to `Ingredient` rather than `VendorIngredient` I will modify my answer to fit the new info – David Watts Nov 25 '19 at 15:52
0

Since newer version of EF core stopped providing this pattern: Saving many-to-many relationship in Entity Framework Core, so you have to manage the bridge-table by your own logic.

The best example that I can think of is here: UserManager.AddToRolesAsync from ASP.NET Core IdentityFramework

You can find this 2 methods from above link: AddToRolesAsync and RemoveFromRolesAsync as examples.

So in your case,

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();
}
Dongdong
  • 2,208
  • 19
  • 28