0

I am trying to insert a new entity (Product) with a generic method which works as expected. This entity (Product) has another entity (Supplier) and when I try to insert this same Product with a new Supplier related to it (aka Product.Suppliers.Add(NewSupplier)) it re-inserts an already present product (created with a seeder method) into the database... I know it has to do with the Supplier because when I don't add it and simply insert the Product and doesn't create a duplicate with the same supplier.

Here is more (simplified) information: Product Entity:

public class Product : BaseEntity
{
    public string ProductName { get; set; }

    public virtual ICollection<Supplier> Suppliers { get; set; }

    ... others
}

Supplier Entity:

 public class Supplier:BaseEntity
{

   public string SupplierName { get; set; }

   public virtual ICollection<Product> Products { get; set; }

    ... others
}

I have a ProductSupplier table with SupplierId & ProductId using Fluent API. Can I still use my generic insert with this scenario, if yes how and what am I doing wrong, why is the Supplier I am getting from DB getting re-inserted?

Thank you all for any feedback! Kind regards

UPDATE: I think it is due to the fact that I encapsulate my context in a using statement when performing CRUD actions. I have come across THIS POST

I will take a look next week because I have to implement other functionalities. Feel free to add your 2cents meanwhile :)

UPDATE 2

This is my Generic insert method:

  public static bool Insert<T>(T item) where T: class // here we specify that the <T> object is of type 'class'
    {
        using (ApplicationDbContext ctx = new ApplicationDbContext())
        {                
                ctx.Database.Log = (dbLog => logger.Debug(dbLog));

                ctx.Set<T>().Add(item);
                ctx.SaveChanges();
                return true;                              
        }
    }
Udara Abeythilake
  • 1,215
  • 1
  • 20
  • 31
Dimitri
  • 1,185
  • 2
  • 15
  • 37
  • I have looked at several sources and i am doubting what to do. This for example: https://stackoverflow.com/questions/11646299/entity-framework-duplicate-records-in-many-to-many-relationship , I am wondering if i should use only 1 DbContext, either passing it arround or just not put my Context actions in a using statement so it doesn't get closed, should i get related entities from DB when inserting a new record that has an relation to it and link/attach them to each other and insert the new entity? NOTE that i am using generic insert/update methods. Anyone has suggestions? Thank you! – Dimitri Aug 29 '18 at 06:48
  • Anyone please :s ? Thank you – Dimitri Aug 30 '18 at 18:12

1 Answers1

0

I have tried several approaches but only 1 worked so far so i am going to continue with this one but please feel free to post your (better) suggestion so i can perhaps improve the current solution. What i do is pretty simple, i am quite sure there must be other ways but don't know of them at this moment. Whenever i am creating new entities, before doing any insert/update, i am fetching related entities IF they exist and attach them to the entity to insert, if they don't exist its OK, entity will insert them for me.

Hope this is of any help to anyone. Kind regards

Dimitri
  • 1,185
  • 2
  • 15
  • 37