Implement INotifyPropertyChanged on generated Entity Framework classes

entity-framework wpf xaml

Question

I have an SQL DB and am implementing a WPF UI to update it. If I use EF5 to generate the classes from the DB, how can I implement INotifyPropertyChanged on the generated classes and properties so I can easily bind to them with the UI? Is there an easy way to achieve this?

Thanks

1
14
1/3/2013 2:41:09 AM

Accepted Answer

If you follow the recommended MVVM pattern for WPF, you can treat your generated classes as the Model, and then write ViewModel wrappers that implement INotifyPropertyChanged. The ViewModel classes would access your DB classes and expose properties that you can bind to your UI in XAML.

As noted in your comment, this can result in a lot of work writing boilerplate code, but there are some ways to deal with that. See this question for some ideas.

While more work at first, the MVVM pattern can definitely pay off in the long run if you ever need to do any intermediate formatting or processing, or if you need to change your database classes without affecting the UI.

13
5/23/2017 12:10:21 PM

Popular Answer

I needed to do the same recently but with Winforms. If you don't want to follow the MVVM pattern as suggested by bde you can modify the t4 template to implement INotifyPropertyChanged on your generated entities.

This answer helped me: https://stackoverflow.com/a/12192358/1914530



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow