简体   繁体   English

新子项在Entity Framework 4中插入现有父项

[英]New child inserts existing parent in Entity Framework 4

When I try to add new child relations to an existing parent object, entity tries to insert the parent object as new , instead of updating it as modified . 当我尝试向现有父对象添加新的子关系时, 实体尝试将父对象插入为对象,而不是将其更新为已修改 I get an error saying its trying to insert a duplicate key . 我收到一个错误,说它试图插入一个重复的密钥

This is really stumping me, it wasn't behaving like this until I updated the model with a new relation (via update from database, not code first). 这真的让我感到困惑,直到我用一个新关系更新模型(通过数据库更新,而不是代码优先),它才表现得像这样。 Other tables in the same context, with the same many-to-many pattern do not cause this error! 在相同上下文中具有相同多对多模式的其他表不会导致此错误!

I've found that if I add my child object to the context, carefully setting the ID values, not the navigation properties, it will save fine. 我发现如果我将我的子对象添加到上下文中,仔细设置ID值,而不是导航属性,它将保存正常。 But the fix-up process isn't adding the child object to the navigation collection , wich cause logic issues in other places. 但是修复过程不是将子对象添加到导航集合中 ,这会导致其他地方出现逻辑问题。

My model has a many to many relation like this: 我的模型有很多这样的关系:

option -< optionUnit >- Unit 选项 - <optionUnit> - 单位

Both Option and Unit objects are existing and loaded into the context. Option和Unit对象都存在加载到上下文中。

Normally I would make the bridge table and set the navigation properties like below: 通常我会创建桥表并设置如下导航属性:

bridge = new OptionUnit()
{
    OptionUnitId = Guid.NewGuid(),
    Unit = SelectedUnit,
    Option = SelectedOption,
};
//Context.OptionUnits.Add(bridge); //added to context via the navigation properties

But when I do this time, entity tried to insert SelectedOption as new (even though its entity state is modifed at time of the save, not new or detached). 但是当我这么做的时候,实体试图将SelectedOption插入为新的(即使它的实体状态在保存时被修改,而不是新的或分离的)。

To get around this I've had to make the bridge and set the id's only. 为了解决这个问题,我不得不建立桥梁并设置id。 I get no error on save, but its not automatically added to SelectedOption.OptionUnits via fix-up like I would expect. 我在保存时没有收到任何错误,但它不会像我期望的那样通过修复自动添加到SelectedOption.OptionUnits。 If i add it to the collection manually, I get the save error again. 如果我手动将其添加到集合中,我再次收到保存错误。

bridge = new OptionUnit()
{
    OptionUnitId = Guid.NewGuid(),
    //Unit = SelectedUnit,
    UnitId = SelectedUnit.UnitId,
    //Option = SelectedOption,
    OptionId = SelectedOption.OptionId,
};
Context.OptionUnits.Add(bridge);

This seems to be only happening for relations on the Options Entity. 这似乎只发生在选项实体的关系上。 The other relation, between optionUnit >- Unit has no problems and works as expected. 另外一个关系,在optionUnit> - Unit之间没有任何问题,并按预期工作。

It seems like theres somethign wrong with the relations, but I'm not sure where to look. 对于这种关系来说,这似乎有些不对,但我不确定在哪里看。 I need to know whats causing this behaviour, how to fix it, and how to prevent it from happening in the future.... 我需要知道导致这种行为的原因,如何修复它,以及如何防止它在将来发生....

Incredibly enough, Julie Lerman just had an amazing MSDN article about this exact issue and why it happens. 令人难以置信的是,Julie Lerman刚刚收到了一篇关于这个确切问题及其发生原因的MSDN文章。 You can find the article right here . 你可以在这里找到这篇文章。

In Julie's own words: 用朱莉自己的话说:

The reason it happens is that when you use the DbSet.Add method, not only is the state of the root entity marked “Added,” but everything in the graph that the context was not previously aware of is marked Added as well. 它发生的原因是,当您使用DbSet.Add方法时,不仅将根实体的状态标记为“已添加”,而且上下文之前未知的图中的所有内容都标记为已添加。 Even though the developer may be aware that the object has an existing Id value, Entity Framework honors its EntityState (Added) and creates an Insert database command for the object, regardless of the existing Id. 即使开发人员可能知道该对象具有现有Id值,Entity Framework也会尊重其EntityState(已添加)并为该对象创建Insert数据库命令,而不管现有Id是什么。

So what's happening in a nutshell here is that when you say Context.OptionUnits.Add(bridge); 那么这里简单地说,当你说Context.OptionUnits.Add(bridge); , it also thinks that you are trying to add the Option and Unit objects to it as well. ,它也认为你也试图将Option和Unit对象添加到它。 Instead, you'll need to use the FKs (as she mentions) and then add it. 相反,你需要使用FK(如她提到的那样),然后添加它。

I think that that's what you're trying to do in your second attempt there, but I wonder how you're checking to see if it's in SelectedOption.OptionUnits . 我认为这就是你在第二次尝试时要做的事情,但我想知道你是如何检查它是否在SelectedOption.OptionUnits Are trying to check it right after the save? 保存后是否正在尝试检查? In which case it won't show up since that value is cached in memory when it gets pulled by Entity Framework. 在这种情况下,它不会显示,因为当Entity Framework提取该值时,该值会缓存在内存中。 You would need to try bringing up that object again afterwards. 之后你需要尝试再次提出该对象。

I found the issue. 我发现了这个问题。 After adding a child item, I would run some logic to update some totals. 添加子项后,我会运行一些逻辑来更新一些总计。 The problem was I used RaiseDataMemberChanged("sum") instead of RaisePropertyChanged("sum"). 问题是我使用了RaiseDataMemberChanged(“sum”)而不是RaisePropertyChanged(“sum”)。 RaiseDataMemberChanged() must mark something in the entity as modified, wich was casuing this issue. RaiseDataMemberChanged()必须将实体中的某些内容标记为已修改,并且正在解决此问题。 Thanks IronMan84 for all your help. 感谢IronMan84的所有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架:使用新的父对象更新现有的子对象 - Entity Framework : Updating an existing child object with a new parent object 实体框架通过多对多关系将现有子级添加到新父级 - Entity Framework adding existing Child to a new Parent on a many to many relation 实体框架:将现有子POCO添加到新的父POCO,在DB中创建新子项 - Entity Framework: adding existing child POCO to new Parent POCO, creates new child in DB 实体框架:将子记录添加到现有父记录中 - Entity Framework: Add child record to and existing parent 实体框架自动在父表中插入新行 - Entity Framework automatically inserts new row in parent table 使用 Entity Framework 6 将父实体与数据库中现有的子实体连接起来 - Connect parent entity with existing child entity in database using Entity Framework 6 实体框架插入现有导航属性实体 - Entity Framework inserts existing navigation property entity 实体框架通过多对多关系将现有子级分配给父级 - Entity Framework assign existing child to parent on a many-to-many relation 有时实体框架在将“子”添加到新(父)元素后插入多个“父”行 - Sometimes entity framework inserts multiple “parent” row after adding “children” to a new (parent) element 创建父对象时,Entity Framework正在创建新的子对象 - Entity Framework is creating new child object when parent object is created
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM