Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Entity State

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.

like image 213
JohnathanKong Avatar asked Dec 31 '22 02:12

JohnathanKong


2 Answers

after you call

objectContext.DeleteObject(x),

you can simulate undelete of object x with

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

like image 151
rale Avatar answered Jan 01 '23 15:01

rale


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.

like image 40
Tion Avatar answered Jan 01 '23 16:01

Tion