Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement INotifyPropertyChanged on generated Entity Framework classes

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

like image 536
Steve Avatar asked Jan 03 '13 02:01

Steve


2 Answers

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.

like image 131
WildCrustacean Avatar answered Nov 10 '22 11:11

WildCrustacean


There's a NuGet package called PropertyChanged.Fody that makes adding INotifyPropertyChanged to a classes properties really simple. Once you've installed the package just add the [ImplementPropertyChanged] attribute to any class or partial class and the package will add INotifyPropertyChanged for you.

Here's a simple example of it's use;

using PropertyChanged;

[ImplementPropertyChanged]
public partial class Order
{
}

See GitHub for more information.

like image 23
mark_h Avatar answered Nov 10 '22 12:11

mark_h