DBContext添加/附加事件? [英] DBContext Added/Attached Event?
问题描述
谢谢!
不幸的是没有这样的事件可用,没有扩展点来添加这样的事件。这是我最大的EF失败之一。扩展性=零。
您唯一可以执行的操作是覆盖 SaveChanges
,然后执行 base.SaveChanges
使用 ChangeTracker
来获取所有附加和添加的类型的实体并执行您的逻辑。但是在向上下文附加或添加实体时触发事件并不一样。
EF 4.1 RC. I want to run some code after an entity has been added/attached to the DBContext. Is there an event for this (I can't find one). Basically I want to check if the added/attached entity is of a certain interface and if it is, do some stuff with it. Thanks!
Unfortunatelly there are no such events available and there are no extension points to add such events. That is in my opition one of the biggest EF failure. The extensibility = zero.
The only thing you can do is override SaveChanges
and before executing base.SaveChanges
use ChangeTracker
to get all attached and added entities of your type and execute your logic. But it is not the same as triggering an event when attaching or adding an entity to the context.
这篇关于DBContext添加/附加事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!