Click here to Skip to main content
15,122,379 members
Articles / Programming Languages / C# 4.0
Posted 14 Dec 2014


9 bookmarked

Adding INotifyPropertyChanged to Entity Framework Classes

Rate me:
Please Sign up or sign in to vote.
5.00/5 (13 votes)
14 Dec 2014CPOL2 min read
Automatically implement INotifyPropertyChanged for every property in Entity Framework 6.0 entity classes


This tip offers an easy way to automatically have INotifyPropertyChanged implemented for every property in a given Entity Framework 6.0 entity class.


In the "database first" option for using Entity Framework, Visual Studio generates your entity classes from an existing database. Purists might balk, but sometimes it would be nice if the generated classes implemented INotifyPropertyChanged so you could data-bind directly to them in a WPF application. Unfortunately, Entity Framework 6.0 doesn't implement INPC for you.

Using the Code

First, install a NuGet package called PropertyChanged.Fody. I'll assume you're already familiar with NuGet since it's probably how you added Entity Framework support to your project in the first place. When you install PropertyChanged.Fody, NuGet will also install its dependency package called Fody.

Suppose you have a generated entity class called Customer that looks something like this:

public partial class Customer
    // Various properties...

We don't want to edit that class/file because it's generated. Instead, we'll leverage the fact that it's declared with the "partial" keyword. This allows us to extend the Customer class by placing the following code in a new file (or any file) in the same project.

using PropertyChanged;

public partial class Customer
    // Adding stuff here is optional.

That's it! Just follow the same pattern for any other entity classes that require INotifyPropertyChanged. You can put all the partial classes in one file if you like.

So how does this work? PropertyChanged.Fody is an "IL weaver". Installing the NuGet package injects a call to PropertyChanged.Fody into the project's build process. It's essentially a post-build step that modifies the assembly after it's built. This particular weaver adds an implementation of INotifyPropertyChanged to every class that has the [ImplementPropertyChanged] attribute.

There are other attributes for customizing the behavior of PropertyChanged.Fody but they apply to individual properties and can't be used on the properties in the generated files. However, you may want to use them in your own (regular non-generated) classes. See for details.

In case you're wondering, the final assembly won't have a runtime dependency on PropertyChanged despite the presence of the attribute, using clause, and project reference. Also, using PropertyChanged.Fody (or any of the other Fody plugins) won't break ClickOnce deployment.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
United States United States
Mark Lauritsen has been a software developer for as long as he can remember.

Comments and Discussions

QuestionWill PropertyChanged.Fody work with EDMX files? Pin
Rod at work30-Sep-20 5:23
MemberRod at work30-Sep-20 5:23 
QuestionAm I missing Something? Pin
noblepaulaziz27-May-20 5:08
Membernoblepaulaziz27-May-20 5:08 
QuestionUsing PropertyChanged.Fody with WPF databinding and EF 6.0 Database first. Pin
Member 124896312-May-18 22:42
MemberMember 124896312-May-18 22:42 
SuggestionThe PropertyChanged attribute has changed since the article was posted Pin
Joseph.W.Smith23-Jun-17 4:38
MemberJoseph.W.Smith23-Jun-17 4:38 
Questionnow how to use PropertyChanged on entities? Pin
Cédric Bourgeois2-Dec-16 6:06
MemberCédric Bourgeois2-Dec-16 6:06 
AnswerRe: now how to use PropertyChanged on entities? Pin
Joseph.W.Smith23-Jun-17 4:34
MemberJoseph.W.Smith23-Jun-17 4:34 
GeneralMy vote of 5 Pin
Arkitec3-Jun-16 10:23
professionalArkitec3-Jun-16 10:23 
GeneralMy vote of 5 Pin
L Hills17-Dec-14 5:42
MemberL Hills17-Dec-14 5:42 
GeneralRe: My vote of 5 Pin
MarkLTX17-Dec-14 13:34
MemberMarkLTX17-Dec-14 13:34 
QuestionSample Pin
ctr13214-Dec-14 20:22
Memberctr13214-Dec-14 20:22 
AnswerRe: Sample Pin
MarkLTX16-Dec-14 7:00
MemberMarkLTX16-Dec-14 7:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.