I am trying to come up with a good way to implement the MVVM pattern using Entity-Framework, where my objects are my models. My DataContext
is my view model. This is a small reproduction of the problem.
View
<TextBox Text="{Binding MyText}" />
ViewModel
I have a need to navigate through records from my database. When a button is clicked in the view, the command is sent to the Viewmodel, which executes nextRecord()
. EF does its magic, and _myObject
next row / record from the database
public class myViewModel: INotifyPropertyChanged { private MyEntityObject _myObject; public string MyText { get { return _myObject.MyText; } set { if (_myObject.MyText != value) { _myObject.MyText = value; OnPropertyChanged("MyText"); } } } private void _nextRecord() { _myObject = myEntitiesContext.NextRecord()
Auto-generated entity model
public partial class MyEntityObject { public string MyText { get; set; } }
Because the view does not know the _myObject
change, it does not update when the _myObject
changes. A few approaches that I thought of.
I have not tested moving my objects to the INotifyPropertyChanged
wrapper INotifyPropertyChanged
, but I am afraid to do this because I have many entity objects.
I could call OnPropertyChanged("...")
for all properties, but some of my entities have many properties for them, which would be ugly. You can use reflection to make it cleaner, but I may have properties that are not database bound.
I could defer this to the user interface, somehow updating the bindings when I click "Next Entry", but this interrupts MVVM and looks dirty
How can I make the user interface recognize changes from _myObject
?
Shoe source share