How to attach additional action performed on create / update or when changes in table are detected? [duplicate]

你说的曾经没有我的故事 提交于 2019-12-08 08:30:48

问题


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.


回答1:


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

var contextAdapter = ((IObjectContextAdapter)dbcontext);            
contextAdapter.ObjectContext
          .ObjectStateManager
          .ObjectStateManagerChanged += ObjectStateManagerChanged;


来源:https://stackoverflow.com/questions/19790560/how-to-attach-additional-action-performed-on-create-update-or-when-changes-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!