凡使用EF6订阅ObjectMaterialized? [英] Where to subscribe to ObjectMaterialized using EF6?

查看:326
本文介绍了凡使用EF6订阅ObjectMaterialized?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想订阅我的上下文中OnjectMaterialized事件如下这个,像这样:

I'm trying to subscribe my context to the OnjectMaterialized event following this, like so:

((IObjectContextAdapter)this).ObjectContext
                             .ObjectMaterialized += ObjectContext_OnObjectMaterialized;

但我使用EF6并在该帖子中提到的OnContextCreated方法不存在于这个版本。

But I'm using EF6 and the OnContextCreated method mentioned on that post does not exists in this version.

我试图在上下文构造订阅物化事件,但随后,当数据库被删除(这是我们在集成测试经常做),该事件不再订阅。我们试图Database.Delete(后重新订阅),但它并不能工作。

I tried subscribing the materialized event at the context constructor, but then, if the database is deleted (which we do often during integration tests), the event is no longer subscribed. We tried subscribing again after Database.Delete() but it doesn't work either.

所以我的问题是,我应该在哪里正确地使用订阅ObjectMaterialized事件实体框架6?

So my question is, where should I properly subscribe the ObjectMaterialized event using Entity Framework 6?

推荐答案

你能不能简单的子类化背景和订阅在构造函数中的事件吗? (我做这个和它的作品对我的方案,情况因人而异。)

Could you simply subclass the context and subscribe to the event in the constructor? (I've done this and it works for my scenario. YMMV.)

这篇关于凡使用EF6订阅ObjectMaterialized?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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