Entity Framework SaveChanges - Customize Behavior?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 13:24:49

问题


So you instantiate an EF context, push objects in or pull objects out, it tracks the complete state of the object changes (if change tracking on). Up until this point the developer has been responsible for making these modifications. But once SaveChanges is called, all of these records are submitted in bulk and the developer is disenfranchised from the final result, save an error message on error or a successful call on completion.

Is there a way to customize the SaveChanges process so that it's not such a black box? Ideally, being able to customize the process would really open up things for me, especially with my application architecture.

Thanks.


回答1:


Handling SavingChanges event is one way but for more complex handling you can override SaveChanges operation itself in your derived context. The difference is that in SavingChanges you can put custom logic before SaveChanges do its job but when overriding SaveChanges you can put custom logic before and after calling base.SaveChanges. There is not better support for custom logic during saving. Using custom SQL for saving is only possible if you map stored procedures to data modification operations of your entities.



来源:https://stackoverflow.com/questions/6797895/entity-framework-savechanges-customize-behavior

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