实体框架的SaveChanges - 自定义的行为呢?自定义、实体、框架、行为

由网友(褪墨残香伊憔悴)分享简介:所以,你实例化一个EF背景下,推入物体或物体拉出来,它跟踪的对象更改的完整状态(如果更改跟踪)。直到此时开发商一直负责制定这些修改。但是,一旦调用SaveChanges被调用时,所有的这些记录都提交的散装和开发人员从最终的结果被剥夺公民权,节省错误的错误消息或完成一个成功的电话。So you instantiate...

所以,你实例化一个EF背景下,推入物体或物体拉出来,它跟踪的对象更改的完整状态(如果更改跟踪)。直到此时开发商一直负责制定这些修改。但是,一旦调用SaveChanges被调用时,所有的这些记录都提交的散装和开发人员从最终的结果被剥夺公民权,节省错误的错误消息或完成一个成功的电话。

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.

有没有一种方法,以便它不是这样的黑盒子定制的SaveChanges过程?理想的情况下,能够自定义过程将真正打开的事情对我来说,特别是我的应用程序架构。

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.

感谢。

推荐答案

办理 SavingChanges 事件是一种方式,但对于更复杂的处理,你可以覆盖调用SaveChanges 操作本身在派生上下文中。所不同的是,在 SavingChanges 你可以把自定义的逻辑之前的SaveChanges 完成其工作,但覆盖在的SaveChanges 你可以把自定义的逻辑之前,并呼吁在 base.SaveChanges 。没有保存过程中的自定义逻辑更好的支持。使用自定义的SQL储蓄仅是可能的,如果您的地图存储过程的实体的数据修改操作。

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.

阅读全文

相关推荐

最新文章