Handle Entity Framework On Create POCO

≯℡__Kan透↙ 提交于 2019-12-09 02:53:30

问题


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.

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.

Thanks very much.


回答1:


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;

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.



来源:https://stackoverflow.com/questions/4765101/handle-entity-framework-on-create-poco

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!