1

I am using Entity Framework and when I insert or update Row in Table I would like to perform additional action. Not only stored procedure from database but actual function in code - preferably event based. (asynchronous execution) Both scenarios should trigger sending some message to a handler or in other way enable execution of a function.

1) create

var Row = DbContext.Table.GetById(Id);    
Row.SomeColumn = newValue;
DbContext.SaveChanges();

2) update

var Row = new Row(valOfColumn1, ...);
DbContext.Table.AddObject(Row);
DbContext.SaveChanges();

Before you answer:

Finding places when currently updates and inserts are performed does not cover future usages which should always be connected with execution of mentioned extra function.

Establishing that one should always create or update Table with use of dedicated function is not an option as I need to assume that people will not be aware of necessity of usage mentioned extra function.

pinckerman
  • 4,115
  • 6
  • 33
  • 42

1 Answers1

0

From this answer, you can add an event handler to the ObjectStateManager:

var contextAdapter = ((IObjectContextAdapter)dbcontext);            
contextAdapter.ObjectContext
          .ObjectStateManager
          .ObjectStateManagerChanged += ObjectStateManagerChanged;
Community
  • 1
  • 1
Mansfield
  • 14,445
  • 18
  • 76
  • 112
  • That's cool but it is in EF 6. Are you aware of any possibiliries to do it in older versions EF 4 is the current and in ongoing project such a change is not welcome. – Adrian Szymczak Nov 05 '13 at 14:25
  • @user2148483 That question is tagged with EF 4.1 and that answer was posted in 2011 before EF5 even came out.... – Mansfield Nov 05 '13 at 14:31
  • Cool, thanks.I would like to ensure that mentioned extra function will be called after changes will be introduced to DB and after post insert script will be executed. I see that event SavingChanges according to documentation “Occurs when changes are saved to the datasource” but how about procedure called after update / create - does it count (stored procedure have to start after creation / update and finish before calling mentioned function) – Adrian Szymczak Nov 05 '13 at 15:06
  • @user2148483 You have an sproc called from a trigger or something? That function will be called once savechanges finishes, it looks like. You'll have to examine the changed entities to figure out whether the change was a creation or update. – Mansfield Nov 05 '13 at 15:11
  • It's not stored procedure or function and it is not possible to implement on the database server. I know how to examine what action was performed and what to do inside, but I have to be sure that not only rows were updated or created but also that stored procedures called on create or update finished execution - and I don't know if savechanges finishes whet it happen or before and than function will be executed faster what is not an option – Adrian Szymczak Nov 05 '13 at 15:19
  • @user2148483 So you want to be sure that *triggers* finish before you execute your code? I don't know whether the SavingChanges event will wait for triggers. My suspicion is yes, but you could always test by making a trigger wait ten seconds or something and see when the SavingChanges event is thrown. – Mansfield Nov 05 '13 at 15:25
  • @user2148483: ObjectStateManagerChanged gets called before anything is sent to the database, before SaveChanges is even called, if adding something new. It is fired when the manager changes, which means when the in-memory representation of the data changes (AFAICT). – Josh Sep 24 '14 at 16:23