none
INotifyPropertyChanged in POCO entities

    Question

  • Hello. I have the question on how to implement INotifyPropertyChanged in POCO entities. I need this for binding. I use EF4.1, code first.

    I don't want to do it manually. I thought it should be possible because EF has change tracking possibilities.

    What I've tried is:

    1. Change proxy generation to include INotifyPropertyChanged - seems to be impossible.

    2. Inherit all entities from base class, implementing INotifyPropertyChanged, and then subscribe to some change tracking events of context - I have not found any events to subscribe. 

    3. IEntityWithChangeTracker seem to provide interface to inject my own change tracker - but I don't know how to get the one from my entities - proxies are created, but they do not implement IEntityWithChangeTracker. I followed http://msdn.microsoft.com/en-us/library/dd468057.aspx

     

    I don't want to go with codegeneration, because I would have to modify codegenerator. Creating my own proxies on the boundary of my Context is also possible, but it complicates change tracking, etc. 

    That really sucks, I expected some elegant solutions in new EF and don't wanna go with nasty solutions like codegeneration or my own proxying.

    Anyone has some suggestions please?


    Thursday, April 14, 2011 8:39 AM

Answers

  • Hello,

    if you want to use code first approach you must do it manually.

    1. Proxy generation is hardcoded and currently you can't change the generated code.
    2. There is no event to subscribe which would be fired when property in entity is changed. That is actually purpose of INotifyPropertyChanged.
    3. IEntityWithChangeTracker is not implemented by proxies. It is for example impelemented by Self Tracking Entities (special type of POCOs generated by T4 template from EDMX).

    So you must either add INotifyPropertyChanged manually or you should give up with code frist, use EDMX and modify T4 template for POCO generation.

    Best regards,
    Ladislav


    Thursday, April 14, 2011 8:49 AM
  • Hello,

    I'm sorry, it should be can't. I corrected my answer. I don't know if such feature is planned by ADO.NET team, but imho any form of extensibility would be awesome. I think, you can make a proposal on MS Connect.

    Best regards,
    Ladislav

    Thursday, April 14, 2011 10:34 AM

All replies

  • Hello,

    if you want to use code first approach you must do it manually.

    1. Proxy generation is hardcoded and currently you can't change the generated code.
    2. There is no event to subscribe which would be fired when property in entity is changed. That is actually purpose of INotifyPropertyChanged.
    3. IEntityWithChangeTracker is not implemented by proxies. It is for example impelemented by Self Tracking Entities (special type of POCOs generated by T4 template from EDMX).

    So you must either add INotifyPropertyChanged manually or you should give up with code frist, use EDMX and modify T4 template for POCO generation.

    Best regards,
    Ladislav


    Thursday, April 14, 2011 8:49 AM
  • Hi Ladislav,

     

    Thank you for so quick answer. Pity... :(

    But shouldn't it be "can't" instead of "can" in the first point of your answer?

     

    Don't you know, is EF team going to support some of these? How can I make a proposal?

     

    Best regards

    Thursday, April 14, 2011 9:24 AM
  • Hello,

    I'm sorry, it should be can't. I corrected my answer. I don't know if such feature is planned by ADO.NET team, but imho any form of extensibility would be awesome. I think, you can make a proposal on MS Connect.

    Best regards,
    Ladislav

    Thursday, April 14, 2011 10:34 AM
  • Hi EF team.

    Do you have any updates in new EF5/6 on proxy interception to write my own proxy code?

    Thank you

    George

    Thursday, July 04, 2013 3:59 PM