如何从试图保存/插入子对象停止实体框架? [英] How do I stop Entity Framework from trying to save/insert child objects?

查看:76
本文介绍了如何从试图保存/插入子对象停止实体框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我保存一个实体与实体框架,我自然认为它只会尝试保存指定的实体。然而,它也试图挽救该实体的子实体。这是造成种种的完整性问题。我该怎么办强制EF仅保存我想保存,因此忽略所有子对象的实体?

When I save an entity with entity framework, I naturally assumed it would only try to save the specified entity. However, it is also trying to save that entity's child entities. This is causing all sorts of integrity problems. How do I force EF to only save the entity I want to save and therefore ignore all child objects?

如果我手动设置属性为null,我得到一个错误操作失败:关系不能被改变,因为一个或多个外键的属性是不可为空。这是因为我设置的子对象专门空,因此EF会息事宁人非常适得其反。

If I manually set the properties to null, I get an error "The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable." This is extremely counter-productive since I set the child object to null specifically so EF would leave it alone.

为什么我不希望保存/插入子对象?

由于这是正在讨论来回的意见,我给的,为什么我要我的孩子对象单独留下一定的理由

Since this is being discussed back and forth in the comments, I'll give some justification of why I want my child objects left alone.

在我建立的应用,EF对象模型不被从数据库加载,但作为在解析平面文件,我敢填充数据对象。在子对象的情况下,许多这些参考查找定义父表中的各种属性的表。例如,主实体的地理位置。

In the application I'm building, the EF object model is not being loaded from the database but used as data objects which I'm populating while parsing a flat file. In the case of the child objects, many of these refer to lookup tables defining various properties of the parent table. For example, the geographic location of the primary entity.

自从我填充这些对象自己,EF假定这些都是新的对象,需要与父沿插入目的。然而,这些定义已经存在,我不希望在数据库中创建重复。我只用EF对象进行查找和填充在我的主表的实体外键。

Since I've populated these objects myself, EF assumes these are new objects and need to be inserted along with the parent object. However, these definitions already exist and I don't want to create duplicates in the database. I only use the EF object to do a lookup and populate the foreign key in my main table entity.

即使是真实数据的子对象,我需要拯救父母放在第一位,并获得一个主键或EF似乎只是把事情搞得一团糟。希望这给出了一些解释。

Even with the child objects that are real data, I needs to save the parent first and get a primary key or EF just seems to make a mess of things. Hope this gives some explanation.

推荐答案

据我所知,你有两个选择。

As far as I know, you have two options.

NULL的所有子对象,这将确保EF不添加任何东西。它也不会删除你的数据库什么。

Null all the child objects, this will ensure EF not to add anything. It will also not delete anything from your database.

设置子对象为使用下面的代码从上下文脱离

Set the child objects as detached from the context using the following code

 context.Entry(yourObject).State = EntityState.Detached

请注意,你不能脱离一个列表 / 收藏。你将不得不遍历您的列表,并在列表中分离每一个项目,像这样

Note that you can not detach a List/Collection. You will have to loop over your list and detach each item in your list like so

foreach (var item in properties)
{
     db.Entry(item).State = EntityState.Detached;
}

这篇关于如何从试图保存/插入子对象停止实体框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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