I am using Entity Framework and Caliburn.Micro to implement an MVVM application.
Basically, I have set up AuthorModel and BookModel in a one to many relationship - a
As Eben mentions, the Author
referenced by the ItemsSource
will be a different object (although it happens to reference the same entity).
I think your approach of using a new DbContext
for both of your windows is correct, you can run into problems if you have persisting/shared DbContexts
; it makes sense to load an EditWindow
with a new context, perform your edits, and dispose of the context.
One option is to Detach
your Book
entity from the old DbContext
, and Attach
it to your new context: (a good explanation here Entity Framework 4 - AddObject vs Attach).
I would probably just favour using the passed Book
entity to reload the selected Book
from the new context e.g. using DbSet<TEntity>.Find, and using that retrieved entity to bind to the SelectedItem
(rather than the one you're passing across windows).
Yes it is. Because the author in ItemsSource is referencing to a different object although the content is the same as the one that is bind to SelectedItem.
I don't know much about EF, I guess you can use the same context for the two entities. Or override the equals (and gethashcode) of the Author class to compare the content then return true if the same.