不同的EF 4 SaveOptions对ObjectContext有什么影响? [英] What effect do the different EF 4 SaveOptions have on the ObjectContext?

查看:118
本文介绍了不同的EF 4 SaveOptions对ObjectContext有什么影响?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解决与这里概述的错误非常相似的错误:



在.NET Entity框架中调用SaveChanges时,InvalidOperationException



看来,解决方案(我还没有尝试,不可否认)是将System.Data.Objects.SaveOptions.None作为SaveChanges()方法的SaveOptions参数传递。



所以在我这样做之前,我正在试图明白不同的SaveOptions是如何工作的(None,AcceptAllChangesAfterSave,DetectAllChanges)。我还没有找到一个清楚的解释,但我也不知道默认是什么。有人可以澄清吗



谢谢!



更新:我在此发布了实际的问题问题:在尝试使用EF 4迭代添加对象时, System.InvalidOperationException

好问题(+1)。



简而言之(从我的理解):



SaveOptions.DetectChangesBeforeSave :这是默认值。当您 ObjectContext.SaveChanges()时,方法 DetectChanges()被调用以在同步的附件实体中OSM。



SaveOptions.AcceptAllChangesAfterSave :当您执行 ObjectContext.SaveChanges(),方法 AcceptAllChanges()被调用 - 这是OSM的内容,图中的实体被迭代,地址和设置为不变/分离。



SaveOptions.None :当您执行 ObjectContext.SaveChanges(),更改ar e绝对保存 - 根本没有同步。图表中的任何东西都将被保存。



根据我的经验,我并没有搞砸这个 - 我把它留作默认(DetectChangesBeforeSave)。 p>

有时候用POCO我听说你需要明确地调用 DetectChanges ,但是我从来没有看过推荐/解决方案将SaveOptions更改为none。



您确定该问题的解决方案是将SaveOptions设置为none?也许你应该提供细节(或问一个单独的问题)你的错误,因为这样的改变会影响你的整个持久层。


I'm trying to resolve an error very similar to the one outlined here:

InvalidOperationException when calling SaveChanges in .NET Entity framework

It appears that the solution (which I have not tried yet, admittedly) is to pass System.Data.Objects.SaveOptions.None as the SaveOptions parameter for the SaveChanges() method.

So before I do that, I'm trying to understand exactly how the different SaveOptions work (None, AcceptAllChangesAfterSave, DetectAllChanges). I haven't been able to find a clear explanation of it however, nor am I sure what the default is. Can anyone clarify?

Thanks!

UPDATE: I have posted the actual problem question here: System.InvalidOperationException when trying to iteratively add objects using EF 4

解决方案

Good question (+1).

In a nutshell (from what i understand):

SaveOptions.DetectChangesBeforeSave : this is the default. When you do ObjectContext.SaveChanges(), the method DetectChanges() is called to synchronized attach entities in the OSM.

SaveOptions.AcceptAllChangesAfterSave : When you do ObjectContext.SaveChanges(), the method AcceptAllChanges() is called - which is the guts of the OSM, where the entities in the graph are iterated, addresses and set to Unchanged/Detached.

SaveOptions.None : When you do ObjectContext.SaveChanges(), changes are saved immeditately - no synchronization at all. Whatever is in the graph is what will be saved.

In my experience i have not messed with this - i've left it as the default (DetectChangesBeforeSave).

Sometimes with POCO's i have heard you need to explicitly call DetectChanges, but i've never seen a recommendation/solution to change the SaveOptions to none.

Are you sure the solution in that question is to set SaveOptions to none? Maybe you should provide detail (or ask a seperate question) as to the error your getting, as a change like this will affect your entire persistence layer.

这篇关于不同的EF 4 SaveOptions对ObjectContext有什么影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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