0

I am having this strange behavior all of a sudden (i have compared my files in version control (tfs) to be sure i did not change anything and i didn't found anything different).

I am seeding my database with some metadata and i see that it has a very strange behavior i never saw before. I am inserting a Entity "Product" and it inserts this entity 2 times, first insert is correct and has everything it should have, the other one has NULL properties (string values) but some (like datetimes) have values.

I have totally no clue why this is happening, it is occurring when i call the base.Seed(ctx); method, that i am sure since i stopped the Webapp after this before it reached anything else.

This entity Product has related Entities, which all other data is created correctly in my tables. Nothing is wrong except this Product.

I tried to only seed 1 product entity instead of adding other as well, same results. I oversaw something: there was still other Entities being seeded so i went and see where it occurred, it was when adding the PurchasePrice in the picture that it happened:

My Product Entity:

public class Product : BaseEntity
{
   public  ICollection<Supplier> Suppliers { get; set; }
   public  ICollection<PurchasePrice> PurchasePrices { get; set; }
}

My Supplier Entity:

public class Supplier : BaseEntity
{
   public ICollection<PurchasePrice> PurchasePrices { get; set; }
   public  ICollection<Product> Products { get; set; }
}

My PurchasePrice Entity:

public  class PurchasePrice:BaseEntity
{
   public decimal Value { get; set; }   
   public Supplier Supplier { get; set; }
   public Product Product { get; set; }
}

The Seeding:

Supplier supplier1 = new Supplier("Microsoft", "Microsoft is the best supplier but its expensive", "btw nummer", "0800-123456", "microsoft@email.com", "contact person name");
ctx.Suppliers.Add(supplier1);

PurchasePrice purchaseprice = new PurchasePrice((decimal)17.70, supplier1);
ctx.PurchasePrices.Add(purchaseprice);

Product product1 = new Product("test product 1", supplier1, purchaseprice);
ctx.Products.Add(product1);

base.Seed(ctx);

No idea where i should look because nothing changed in my model, neither in my way of seeding. I tried using AddOrUpdate() but that didn't worked.

I am using EF6 in a MVC web app using Code-first approach no migrations(yet). Anyone has any suggestion please?

Dimitri
  • 1,185
  • 2
  • 15
  • 37
  • Editted the original post – Dimitri Dec 06 '18 at 08:01
  • Anyone has a suggestion, am i overlooking a relation here that is causing this? Kind regards – Dimitri Dec 10 '18 at 08:55
  • 1
    There are many questions somewhat similar see: https://stackoverflow.com/search?q=duplicate+entity+framework – NoChance Dec 10 '18 at 13:52
  • @NoChance, i have tried many of these but none of them worked for me, i want to remind that this happens on Seeding data, inheriting from DropCreateDatabaseAlways, so this should not create dupes unless the relation is not correct but even than, it shouldn't create null values for the dupes (am i correct?) – Dimitri Dec 10 '18 at 14:07
  • What is "DropCreateDatabaseAlways"? – NoChance Dec 10 '18 at 14:20
  • @NoChance, From Microsoft Docs: "An implementation of IDatabaseInitializer that will always recreate and optionally re-seed the database the first time that a context is used in the app domain. To seed the database, create a derived class and override the Seed method" => https://learn.microsoft.com/en-us/dotnet/api/system.data.entity.dropcreatedatabasealways-1?redirectedfrom=MSDN&view=entity-framework-6.2.0 – Dimitri Dec 10 '18 at 14:39
  • 1
    have you call this Database.SetInitializer() or not. – Trilok Kumar Dec 10 '18 at 14:59
  • @TrilokKumar, yes i do, in my DbContext constructor i have added this: Database.SetInitializer(new NameOfMySeeder()); where NameOfMySeeder is inheriting from 'DropCreateDatabaseAlways' and in that i am Seeding my data. – Dimitri Dec 10 '18 at 15:06
  • 1
    I suggest you do a debug and watch the table carefully. This may show you the method responsible for the duplication. It could happen due to inheritance, such that when you create a new object, a method is called implicitly. – NoChance Dec 10 '18 at 16:03
  • @NoChance (and others), i have found a way to not create duplicates, but this is requiring to change my model, i don't think i fully understand how EF is reacting and don't know if i should change my model in that way... https://stackoverflow.com/questions/53706984/entity-framework-inserting-duplicates-on-seeding-database?noredirect=1#comment94280993_53706984 Changing the relation in the PurchasePrice Class of the Entity Product to a ICollection instead of 1 Single Product doesn't create a dupe... I will close this one, create a new post with another question regarding this. Thank you all! – Dimitri Dec 11 '18 at 09:15
  • In such a case, the row is implicitly created when you created a price row. Is this correct? – NoChance Dec 11 '18 at 09:50
  • Creating the Price row itself doesn't, but adding it as a reference to the Product does. – Dimitri Dec 11 '18 at 09:59

1 Answers1

0

EDIT I have created a test app to just be able to test things out and be 100%. I do not yet know why but i have the same relation as before (Price Entities only having 1 Product reference (that was creating a duplicate)) and i don't have a duplicate...

So i can have the relation i want which is 1 Price should only have 1 Product reference but i have absolutely no idea what is happening here ...

Changing the relation in the PurchasePrice Class of the Entity Product to a ICollection instead of 1 Single Product doesn't create a dupe (and creates a PurchasePriceProduct table).

Seems from the database logs (log4net) that due to the relation EF is first inserting a Product (NULL) for the PurchasePrice Reference of the Product , AND inserts the Product (NOT NULL) with its references ... (If anyone needs any clarification on this let me know ill do my best)

This post HAS MOVED TO HERE. I want to thank everyone that has contributed in any way!

Dimitri
  • 1,185
  • 2
  • 15
  • 37