0

I am trying to implement a way to either shelve changes to an EF6 context while I dispose and reload it or to refresh the context without losing changes.

I have a lot of scenarios where the user is updating a foreign key relationship on an object and they need to refresh the combobox on the form with new data without resetting all of their work.

For example: The object starts as follows:

Foo.Name = "Joe"
Foo.Bar = SomeBar

The user wants to change the object to

Foo.Name = "Sam"
Foo.Bar = NewBar

But they can't because NewBar needs to be created. So, the user opens the Bar creation form (which is using a different EF context) and makes NewBar. When they return to the Foo edit form, they expect to see NewBar in the dropdown, but it is not there. They can close and reopen the form to see NewBar, but now they need to change "Joe" to "Sam" again.

How can I get around this or implement a pattern that makes it a non-issue?

MrZander
  • 3,031
  • 1
  • 26
  • 50
  • Please include your code to better understand your question. – Eriawan Kusumawardhono Dec 29 '15 at 23:02
  • @EriawanKusumawardhono I don't have an explicit code example that would make this clearer than it is. It boils down to being able to cache the changes on an EF context while it is refreshed from the database. – MrZander Dec 29 '15 at 23:08
  • You could try detach the Foo from context then when you create context again attach the Foo again so it will have old data. – Florim Maxhuni Dec 29 '15 at 23:08
  • @FlorimMaxhuni I've tried something similar to that, but I end up having to essentially reload the form by hand. All of the data bound to that context, including the Foo, must be reloaded when the context is disposed. This means I have to re-fill all of the comboboxes manually. I was hoping for a solution that was less cumbersome. – MrZander Dec 29 '15 at 23:15
  • Are you using MVVM pattern? If so, you could reload data for your Bar dropdown after saving NewBar entity. – Patryk Spytkowski Dec 29 '15 at 23:53
  • @PatrykSpytkowski Yes, we are using MVVM. Could you explain how this can help? – MrZander Dec 29 '15 at 23:58
  • If you're using MVVM you've probably bound your dropdown to an ObservableCollection. You could just clear it's content or just add NewBar element. All you need is to detect when new Bar entity is being saved. You could use an event inside Bar creation view model or something like [MVVM Light Messenger](http://stackoverflow.com/questions/16993918/mvvm-light-messenger-sending-and-registering-objects). – Patryk Spytkowski Dec 30 '15 at 00:06

1 Answers1

0

This basically turned out to be a non-issue.

I am using a Messenger to notify my contexts when they need to refresh.

MrZander
  • 3,031
  • 1
  • 26
  • 50