Using INotifyPropertyChanged with Entity Framework 6 DbContext Generator

Dan Bechard picture Dan Bechard · Sep 18, 2014 · Viewed 10.5k times · Source

I know I can use ObjectContext instead, but I like the features of DbContext / DbSet. My application isn't large enough to warrant me writing complex view models, so I'd like to implement change notification on the EF generated models directly.

How can I achieve this?

Answer

markltx picture markltx · Dec 18, 2014

I've had great success using a NuGet package called PropertyChanged.Fody to get INPC implemented on the entity classes. Just install the package, then add the [ImplementPropertyChanged] attribute to any class and PropertyChanged.Fody will "inject" INPC into the class as part of the build process. For example if you have a generated entity class called Customer, just add the following code somewhere in your project.

using PropertyChanged;

[ImplementPropertyChanged]
public partial class Customer
{
}

There are other attributes you can use to control the behavior of the PropertyChanged package. See https://github.com/Fody/PropertyChanged for details.