Handle Entity Framework On Create POCO

前端 未结 1 1397
长情又很酷
长情又很酷 2021-01-12 01:31

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

相关标签:
1条回答
  • 2021-01-12 02:01

    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.

    0 讨论(0)
提交回复
热议问题