Seeding method is inserting additional Entities with NULL values

房东的猫 提交于 2019-12-31 05:14:54

问题


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?


回答1:


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!



来源:https://stackoverflow.com/questions/53634862/seeding-method-is-inserting-additional-entities-with-null-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!