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

查看:92
本文介绍了播种方法是插入具有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).

我在数据库中植入了一些元数据,我发现它具有我从未见过的非常奇怪的行为。我正在插入一个实体 Product,它会插入该实体 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网络应用中使用的是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引用插入了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天全站免登陆