处理实体框架创建POCO [英] Handle Entity Framework On Create POCO

查看:189
本文介绍了处理实体框架创建POCO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想看看有没有办法挂接到Entity Framework上下文,所以一旦创建了一个POCO对象就知道了。

I'd like to see if there's a way to hook into the Entity Framework context so I know as soon as it has finished creating a POCO object.

有什么属性我可以使用,例如与[OnDeserializing]?目的是在上下文从数据库获取中创建时,在对象上设置一些值。

Are there any attributes I can use, such as with [OnDeserializing]? The purpose is to set a few values on the object as soon as the context is done creating it from a database fetch.

非常感谢。

推荐答案

将Hook转换为ObjectContext触发的ObjectMaterialized事件。在CTP5中,您需要在DbContext的构造函数中转换DbContext:

Hook into the ObjectMaterialized event fired by ObjectContext. In CTP5, you need to cast your DbContext like so in the constructor for your DbContext:

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

如果您没有使用代码优先,则不需要转换。然后实现你的函数 ObjectContext_OnObjectMaterialized(object sender,ObjectMaterializedEventArgs e)。通过EventArgs,您将可以访问刚刚实现的对象。

If you are not using Code First, you don't need to cast. Then implement your function ObjectContext_OnObjectMaterialized(object sender, ObjectMaterializedEventArgs e). Via the EventArgs, you will be able to access your object, which has just been materialized.

这篇关于处理实体框架创建POCO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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