EF6 POCO INotifyPropertyChanged without viewmodels

entity-framework-6 inotifypropertychanged poco wpf


I have been binding in WPF application directly to the model classes (and skipping creating individual viewmodel classes).

Now, after switching to EF6 and DBContext, I face an issue with the generated EF POCO classes since it looks its either kind of tricky or not even recommended trying to make INotifyPropertyChanged interface implemented directly to those classes.


  • I don't want to go back to ObjectContext.
  • I don't want to change T4 too much either. The suggestions on the web for changing T4 to achieve INotifyPropertyChanged looks too error-prone for me.
  • Creating viewmodels for each class now and going purely to MVVM would probably be best but takes now lot of time to implement since the model is huge.

Do I have any options left to get EF6 POCO class autogenerated properties to notify of their changes?

9/16/2014 6:23:25 AM

Popular Answer

T4 Templates are your best friends here. You almost can't avoid them Option 1 - Modify your existing T4 templates to implement INotifyPropertyChanged

  1. Create a base class that implements INotifyPropertyChanged
  2. Modify the getter and setters in your T4 templates to notify of their property changes

Option 2 - Introduce DTOs/ViewModels and use AutoMapper

  1. Add a new folder to your project (or create another project)
  2. Add a new POCO generation T4 template
  3. Modify it slightly to conform to your view model of choice
  4. Use AutoMapper to map these Dto/ViewModels to the entities

Option 3 - Implement Postsharp which uses aspect oriented programming to implement INotifyPropertyChanged with a one line attribute per class - again, you'll have to add a couple of lines to your T4 template

EDIT - examples Here's a T4 template with my entities, that I added the [DataContract] attributes to allow the POCOs to be serialized.

foreach (var entity in typeMapper.GetItemsToGenerate<EntityType>(itemCollection))
    fileManager.StartNewFile(entity.Name + ".cs");
<#=codeStringGenerator.UsingDirectives(inHeader: false)#>
    using System.Runtime.Serialization;

// Then further down
    var simpleProperties = typeMapper.GetSimpleProperties(entity);
    if (simpleProperties.Any())
        foreach (var edmProperty in simpleProperties)
9/16/2014 8:12:53 AM

Related Questions


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