Click here to Skip to main content
15,879,239 members
Articles / Programming Languages / C# 4.0
Tip/Trick

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 52.5K   9   11
Automatically implement INotifyPropertyChanged for every property in Entity Framework 6.0 entity classes

Introduction

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

Background

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:

C#
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.

C#
using PropertyChanged;

[ImplementPropertyChanged]
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 https://github.com/Fody/PropertyChanged 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.

License

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


Written By
Software Developer (Senior)
United States United States
Mark Lauritsen has been a software developer since 1983, starting at IBM and using a variety of languages including PL/1, Pascal, REXX, Ada, C/C++ and C#. Mark currently works at a midstream energy company developing Windows services and desktop applications in C#.

Comments and Discussions

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