views:

1358

answers:

2

Hello Everyone,

I currently have an entity model with a bunch of deleted items, the state is deleted. Is there a way to "undelete" them? I know which Items I want to undelete, but I don't know how to undelete the items. Ideally I'd like to get it back to an unchanged state.

A: 

Do you have the option of just not committing the connection context? - dispose the ObjectContext without calling objectContext.SaveChanges(); Of course, if you have certain changes that you do wan't saved, they will not persist either.

If you called objectContext.DeleteObject(x) you can't undelete it and still save changes.

ObjectStateEntry objectStateEntry = objectContext.ObjectStateManager.GetObjectStateEntry(x);

// objectStateEntry.State is not setable

ObjectStateEntry does have the OriginalValues property so you could, in theory, painstakingly recreate a collection that represents the original changes, minus the unwanted ones, exit the objectContext, open a new one and rebuild those changes minus the unwanted ones there. Probably not worth the hassle, but there is no documented way to unmark something for deletion at this time.

Tion
That's too bad. I had a feeling there is no way. Unfortunately we can't just clear out the context, so that's not really an option. Right now I have implemented a list of objects that are to be modified before being removed. It's actually a really big hassle and it's too bad that something can't be marked as unchanged.
JohnathanKong
Entity Framework Design blog http://blogs.msdn.com/efdesign/ says:"...self-tracking entities architecture that will ship along side of VisualStudio 2010 and .NET Framework 4.0.""...There are conveience methods on each self-tracking entity to change this state if needed (Delete() and SetUnchanged())."So it looks like this ability will be available in EF2
Tion
A: 

after you call

objectContext.DeleteObject(x),

you can simulate undelete of object x with

objectContext.Detach(x); objectContext.Attach(x)

rale