views:

42

answers:

2

Is there a method of manually clearing/resetting an ObjectContext back to its initial state? Note that I can't just instantiate a new context.

This is using the 1.0 version of the Entity Framework.

Thanks

+2  A: 

ObjectContext is meant to be a short-lived object, it shouldn't be cached like that. Normal usage should look like:

using(var ctx = new MyContext())
{
    // Select/update/insert/delete
    ctx.SaveChanges();
}
Sander Rijken
I'm aware of how it should be used and in my case it is fairly short-lived. Each context exists for the duration of handling one message sent via NServiceBus. A new context is started with each event handler. In this case though, I'm running into an issue where objects don't seem to be detaching correctly and I want to reset the context to see if that fixes things.
Brian Vallelunga
Well because that's the way it should be used, I don't think there's a way to reset it to the initial state.
Sander Rijken
A: 

You can try to call the Detach method for each entity that was loaded to the context.

Devart