EF ComboBox not displaying SelectedItem Binding

前端 未结 2 1618
孤街浪徒
孤街浪徒 2021-01-06 05:37

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

相关标签:
2条回答
  • 2021-01-06 06:18

    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).

    0 讨论(0)
  • 2021-01-06 06:22

    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.

    0 讨论(0)
提交回复
热议问题