What if I call DbContext.Dispose without calling DbContext.SaveChanges?

I am looking for a way to undo changes to an entity. I came across this answer which shows how to set the state of an entity, but I'm wondering what happens if I just get rid of the instance dbContext

without calling dbContext.SaveChanges()

or manipulating the entity claims.

The code I wrote to do this definitely works, but am I leaving anything in an erratic state by rejecting changes this way?

+3


source to share


3 answers


what happens if i just delete the dbContext instance without calling dbContext.SaveChanges () or manipulating entity states

Nothing. Instances that were previously bound to an already located instance DbContext

continue to exist, as any normal instances assume there is a handle to those instances somewhere somewhere. If not, then the memory will be released and eventually garbage will be collected like any other normal instance of something if there is no descriptor. The state of objects in memory remains as it is, nothing in memory is automatically returned back. The database store also remains "as is", that is, there is no call from the DbContext to the data store.

I leave anything unstable by rejecting changes this way



No, at least not in the data warehouse. It's hard to tell in memory that it depends on where the code stayed and what dependencies were before the modifications up to that point. Suppose this is a stateless asp.net application, perhaps the request just ends, in which case nothing volatile should happen with any of the following requests, as they should fetch whatever is required back from the datastore.

If its something that looks like a Windows application for a longer period of time, you might have to manually ensure that all the instance pointers / handles that were previously tracked are either updated with the currently updated in memory, or those pointers have been released.

As for any new DbContext instances, they all work independently of each other, so there is no continuation between them. The new DbContext is unaware of the tracking state, or the state that was tracked by any other DbContext instance.

+3


source


Calling a method Dispose()

on a class that implements IDisposable

means that you are telling the library "I am done with this object, I will not use it anymore. You can clarify it." This is not the case for the Entity Framework.

For most classes, attempts to continue using the object after the call Dispose()

will fail, sometimes with an explicit exception warning you of the problem, sometimes with an internal exception thrown by the corrupted state of the object. You shouldn't assume Entity Framework is an exception: once called, dbContext.Dispose()

you should no longer use the referenced context dbContext

.

However, there is nothing to prevent you from immediately creating a new context:



dbContext.Dispose();
dbContext = new DbContext();

      

This new context will have absolutely no recollection of any unsaved changes you made in the old context. There are many good cases where this approach is most practical.

+3


source


Nothing will be unstable, so no need to worry. If you try to call the context after you've posted it, you will get ObjectDisposedException

, otherwise it is legal to destroy it if you no longer need it.

0


source







All Articles