Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm looking for something like a listener so i can do function automatically after save change in MVC5
for example :
I have table called Tasks ,
when i call the method
C#
Tasks.Add(task) ;
dbcontext.savechange();

after save i want to call the method Addlog()
which add new log to log table

without need to Call AddLog() method after each insert ,

What I have tried:

some thing like call functions
on model creation , or model updated ...
Posted
Updated 30-Dec-16 4:45am
v2

1 solution

Go into your DbContext implementation and override SaveChanges.

In there, if you call the base.SaveChanges() method FIRST then you can do whatever you want. DO NOT CHANGE MORE STUFF IN THE DATABASE AND CALL SaveChanges FROM INSIDE YOUR SaveChanges METHOD OR YOU WILL END UP IN AN INFINITE LOOP UNTIL YOU CRASH THE APP BY EXHAUSTING THE STACK SPACE OF YOUR APP.

Now, if you make the additional changes BEFORE you call the base.SaveChanges() method, this won't be a problem.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900