在对象实例化实体框架代码第一次回调 [英] Entity Framework Code First callback on object instantiation

查看:210
本文介绍了在对象实例化实体框架代码第一次回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个遥不可及的问题,而是有可能在实体对象,每当一个新的实例已经被从数据库加载(作为如LINQ查询一部分)回调火,一个电话创建或相似?

This may be a far fetched question, but is it possible to have a callback fire in an entity object, whenever a new instance of it has been loaded from the database (as part of e.g. a linq query), a call to Create or similar?

这样的回调的目的是要传达一种背景下,或者设置初始化参数,从封闭的业务对象。

The purpose of such a callback would be to convey a context, or set of initialization parameters, from the enclosing business object.

推荐答案

的DbContext 绝对没有它,但你可以尝试将其转换回的ObjectContext 及用途:

DbContext definitely doesn't have it but you can try to convert it back to ObjectContext and use:

var objectContext = ((IObjectContextAdapter)dbContext).ObjectContext;
objectContext.ObjectMaterialized += ...



从数据库加载对象后,将火(我不知道这是否展销会新创建的对象以及)。它是所有对象的全球性活动,所以你将不得不把一些逻辑到处理程序只对某些类型的运行代码。

It will fire after loading object from database (I'm not sure if it fairs for newly created objects as well). It is global event for all objects so you will have to put some logic into handler to run your code only for some types.

这篇关于在对象实例化实体框架代码第一次回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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