DBContext添加/附加事件? [英] DBContext Added/Attached Event?

查看:124
本文介绍了DBContext添加/附加事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EF 4.1 RC。我想在一个实体添加/附加到DBContext之后运行一些代码。有没有一个事件(我找不到一个)。基本上我想检查添加/附加的实体是否是某个接口,如果是,请做一些事情。
谢谢!

解决方案

不幸的是没有这样的事件可用,没有扩展点来添加这样的事件。这是我最大的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆