Issue with EF child objects

Mar 15, 2012 at 11:55 PM


I'd like to start off with, your framework is pretty amazing!!  It's saved me a lot of work, and gotten me about 90% of the way to where I need to be.  I do have a few issues left, and I'm hoping you can help.

When using entity framework, and have foreign keys mapped it creates relationships which MUF has issues handling.  For example, if you had a parent object, which has a one to many relationship with the child object in EF it will connect them using virtual collections.  ie.  To get to the Child collection of a parent you'd do: Parent.Childs.  I have this mapped to a TreeView, and if you were to delete a child object, and then undo, the item is recreated in the collection, however because the connection to the parent isn't redone you can't see it.   If you were to delete the Parent, the relationships get lost to the children.  I've noticed if i reload the context the datamodel is able to recreate all the links, but that's not a very useable solution in my app.  I'm hoping you can advise me on what I can modify in order to have the collection undo deletes and creates properly.

I have a test app with sample data using EF code first 4.2 and MUF in WPF which I would be happy to send to you which can illustrate the problem a little better than I can in words.  Please let me know if you would like this.

Also - are there any hooks I can do to the undo service to see what's being added to the collection?  The app that this is being used on is rather large so sometimes there will be a lot of things getting to the stack and it would make life a lot easier if I could output anything that's being added to a debug window.  The stacks aren't observable, but is there anything else I could tap into?

Thank you for your help




Mar 16, 2012 at 2:33 AM

Hi Mark!

Thanks for the kind remarks about MUF. Glad that you like the framework.

Regarding your questions about using it with EF, I think I understand your scenario with the parent child situation. There may be a couple options to work with this. But, can you tell me how you are using EF? It sounds like you're using code first. Did you create your own models / entities? Do these entities have "fixup" code to re-connect the bidirectional relationships?

If you can share your test app, that would be great and would help me understand you situation.


Regarding hooks, there are events on the UndoRoot class that are fired after an Undo / Redo, and after adding an item to the UndoStack. These events don't include the change(s) that were added. Just the fact that the stack(s) changed. We could work on adding some additional hooks.

In the mean time, there are a couple options for hooking the changes:

1) The code is MIT licensed, so you're free to pull it down and tweak it in your own project. This might help with debugging things too.

2) You could "wrap" the DefaultChangeFactory. Implement your own version that calls into DefaultChangeFactory, but does your diagnostic interception steps before / after the calls.



Mar 16, 2012 at 9:31 AM

I used code first for the test app, but for the primary application we're using EF with a standard SQL db.  The entities and relationships are actually being generated by EF based on the keys on SQL, but this behavior is simulated well in my smaller application.   What would be the best way for me to send you this?  It's small, about 4 MB, is email ok?

RE: Hooks, Thank you for the suggestion of wrapping the factory, I think this will work perfectly.


Mar 18, 2012 at 11:47 PM

Hi Mark,

I got your sample and reviewed how it worked. I sent back an updated version and some comments on how MUF could be used in your scenario.

Let me know if you have any comments or questions.

- Nathan