none
Is it possible to observe entity changes via DbContext? RRS feed

  • Question

  • Hi,

    Is it possible to have a callback called when the CurrentValues of a DbEntityEntry changes? I need to monitor when the value of a server-generated primary key of an entity has changed when it has been submitted to the database. However, I have no control over when SaveChanges is called on my DbContext and I can also not assume that my entities implement INotifyProperttyChanged. Since, after SaveChanges is called, the Currentvalues of my newly created entities will change, I was hoping there is some way to observe any changes the CurrentValues collection.

    Any help/suggestions ?

    Kind regards,

    Merijn

    Wednesday, June 6, 2012 7:51 PM

Answers

  • Hi MdeJ,

    Welcome to MSDN Forum. ObjectContext has a SavingChanges event. We can realize our logic when this event is fired. We can access ObjectContext from DBContext instance in this way:

    ((IObjectContextAdapter)context).ObjectContext

    More information about this event, please refer here.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by MdeJ Friday, June 8, 2012 7:50 AM
    Thursday, June 7, 2012 5:55 AM
    Moderator

All replies

  • Hi MdeJ,

    Welcome to MSDN Forum. ObjectContext has a SavingChanges event. We can realize our logic when this event is fired. We can access ObjectContext from DBContext instance in this way:

    ((IObjectContextAdapter)context).ObjectContext

    More information about this event, please refer here.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by MdeJ Friday, June 8, 2012 7:50 AM
    Thursday, June 7, 2012 5:55 AM
    Moderator
  • Thanks, that helps.
    Friday, June 8, 2012 7:50 AM