问题
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