2

I have two entities connected via one-to-many relationship: Computer entity has many Instances. I'm trying to develop simple undo/redo mechanism for my appliation.

Redo for DeleteComputerAction just removes computer instance from Entity Framework context (it also clears computer's Instances collection). But when I want to Undo this change by

((IObjectContextAdapter)_dbContext).ObjectContext.Refresh(RefreshMode.StoreWins, computer.Instances);
_dbContext.Entry(computer).Collection(e => e.Instances).Load();

nothing happens. May be EF caches the data I want to reload? How can I force to reaload computer.Instances collection from the DB?

EDIT

Actually I have no changes in my DB. I just mark computer entity as deleted in EF context and then immediately try to restore its state form DB. When I delete computer entry from EF context its .Instances collection becomes empty. I just need to automatic repopulate it from DB and that is all.

Sergey
  • 161
  • 9
  • http://stackoverflow.com/questions/2331225/how-to-refresh-objectcontext-cache-from-db – hawkstrider Oct 09 '15 at 14:26
  • I do refresh, but computer.Instances collection is empty. Actually I have no changes in my DB. I just mark computer entity as deleted in EF context and then immediately try to restore its state form DB. When I delete computer entry from EF context its .Instances collection becomes empty. I just need to automatic repopulate it from DB and that is all – Sergey Oct 09 '15 at 14:37
  • I work with old desktop application which has single context during application's lifetime (yes, it is terrible), and I have no chance to dispose context without breaking all the app. – Sergey Oct 09 '15 at 14:50
  • Why don't you simply dispose context and create a new one ? Also in scenario like the one you have it's better not to delete anything from context *before* delete operation is confirmed but rather use *transactions*. You can create collection and put all items that are marked to delete inside, then if it is confirmed execute query that deletes these items from context and underlying database. – Fabjan Oct 09 '15 at 14:52
  • If you look at the comment on the post that I linked it looks like he had to call ToList() on the collection to get the newly loaded entries. That sounds a little hokey to me, but perhaps worth a try. – hawkstrider Oct 10 '15 at 16:20

0 Answers0