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

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

问题描述

当我使用实体框架保存实体时,我很自然地认为它只会尝试保存指定的实体.但是,它也在尝试保存该实体的子实体.这导致了各种完整性问题.如何强制 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

请注意,您不能分离 List/Collection.您将不得不遍历列表并像这样分离列表中的每个项目

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天全站免登陆