31

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

Community
  • 1
  • 1
morganpdx
  • 1,856
  • 2
  • 29
  • 46

2 Answers2

18

Short correction for

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

SaveChanges is an overloaded version of SaveChanges(SaveOptions optsions) method, where this parameterless version calls this

SaveChanges(SaveOptions.DetectChangesBeforeSave | SaveOptions.AcceptAllChangesAfterSave)

SaveOptions is a Flag enum and in conclusion, SaveOptions.DetectChangesBeforeSave | SaveOptions.AcceptAllChangesAfterSave is the default of SaveChanges() not the DetectChangesBeforeSave

D3M80L
  • 191
  • 1
  • 3
17

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 changed this - I've left it as the default (DetectChangesBeforeSave).

Sometimes with POCOs 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 separate question) as to the error you're getting, as a change like this will affect your entire persistence layer.

halfer
  • 19,824
  • 17
  • 99
  • 186
RPM1984
  • 72,246
  • 58
  • 225
  • 350
  • Thanks - exactly my reason for asking the question, as it will affect my entire application to just fix this one issue. I'm pretty reluctant to do that with a solution I don't fully understand. What are you referring to when you say OSM? EDIT: ObjectStateManager. Got it. :) – morganpdx Jan 12 '11 at 22:49
  • And yeah, I should post a question, since this is the only solution stated in the other problem that claims to work that would apply in my case. But it appears to be the exact same problem. – morganpdx Jan 12 '11 at 22:50
  • @morganpdx - how do you have EF setup? are you using default code generation, or are you using POCO's? And if so - are you using any change tracking (e.g self tracking entities, proxy objects, etc). Post a question with this info and the error/scenario/problem you are having. – RPM1984 Jan 12 '11 at 23:25
  • Nope, using default code generation. I will definetely post the question tonight or first thing tomorrow. I have a suspicion that there might be some bogus entities stuck in the OSM - trying to test that theory first. – morganpdx Jan 12 '11 at 23:39
  • 1
    Oh and incidentally, great answer! :) – morganpdx Jan 12 '11 at 23:39
  • Actually entities will automatically inspect changes. So DetectChanges() is useless. Another point is POCO models. More information is in EF development blog: http://blogs.msdn.com/b/adonet/archive/2009/06/10/poco-in-the-entity-framework-part-3-change-tracking-with-poco.aspx – Artur A Feb 22 '13 at 14:41