播种方法是插入具有 NULL 值的附加实体 [英] Seeding method is inserting additional Entities with NULL values

查看:14
本文介绍了播种方法是插入具有 NULL 值的附加实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我突然出现了这种奇怪的行为(我在版本控制 (tfs) 中比较了我的文件,以确保我没有更改任何内容并且我没有发现任何不同之处).

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).

我正在使用一些元数据为我的数据库播种,我发现它有一种我以前从未见过的非常奇怪的行为.我正在插入一个实体产品",它插入了这个实体 2 次,第一个插入是正确的并且具有它应该具有的一切,另一个具有 NULL 属性(字符串值)但有些(如日期时间)有价值观.

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.

我完全不知道为什么会这样,当我调用 base.Seed(ctx);方法,我敢肯定,因为我在此之后停止了 Webapp,然后才到达其他任何地方.

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.

我尝试只播种 1 个产品实体,而不是添加其他产品实体,结果相同. 我监督了一些事情:还有其他实体正在播种,所以我去看看它发生在哪里,它是在发生的图片中添加 PurchasePrice 时:

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:

我的产品实体:

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

我的供应商实体:

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

我的 PurchasePrice 实体:

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

播种:

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

不知道我应该看哪里,因为我的模型没有任何改变,我的播种方式也没有改变.我尝试使用 AddOrUpdate() 但没有奏效.

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.

我在 MVC Web 应用程序中使用 EF6,使用代码优先方法(尚未).请问大家有什么建议吗?

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

推荐答案

编辑我创建了一个测试应用程序,以便能够测试并达到 100%.我还不知道为什么,但我的关系和以前一样(价格实体只有 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...

所以我可以拥有我想要的关系,即 1 价格应该只有 1 个产品参考,但我完全不知道这里发生了什么......

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 ...

将实体产品的 PurchasePrice 类中的关系更改为 ICollection 而不是 1 个单一产品不会创建一个副本(并创建一个 PurchasePriceProduct 表).

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).

从数据库日志 (log4net) 看来,由于关系 EF 首先为 Product 的 PurchasePrice Reference 插入 Product (NULL),并插入 Product (NOT NULL) 及其引用 ...(如果有人需要对此进行任何澄清,让我知道我会尽力而为)

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)

这篇文章已移至此处.我要感谢所有以任何方式做出贡献的人!

这篇关于播种方法是插入具有 NULL 值的附加实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆