我可以访问实体IDbCommandInterceptor实体框架 [英] Can I access the entity in IDbCommandInterceptor in Entity Framework

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

问题描述

在实施IDbCommandInterceptor,我可以访问已为命令/查询创建SQL命令。是它也可以以访问正在持续/同时检索中所实现的方法的实际实体对象?



下面是一些幻想的代码来演示我想做些什么:

 公共类WidgetInterceptor:IDbCommandInterceptor 
{
公共无效NonQueryExecuting(System.Data.Common.DbCommand命令,DbCommandInterceptionContext< INT> interceptionContext)
{
VAR部件= interceptionContext。实体窗口小部件;

如果(小部件!= NULL)
} $ B $
{它被保存...

//执行小窗口任务} b}


解决方案

在查询的情况下,有没有一个实体。在执行SQL命令可以返回任意数量的行可能导致物化实体或某种投射。您可以访问的DbContext或ObjectContext的参与,并通过其ChangeTracker查看其中的实体的状态,但据我所知有关联你与任何特定的实体执行特定的命令没有直接的方法。根据你的情况下,数量,类型,并在ChangeTracker实体的状态可能足以做你所追求的。


When implementing IDbCommandInterceptor, I can get access to the SQL command that has been created for the command/query. Is it also possible to get access to the actual entity object that is being persisted/retrieved whilst in the implemented methods?

Here is some fantasy code to demonstrate what I would like to do:

public class WidgetInterceptor : IDbCommandInterceptor
{
    public void NonQueryExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
    {
        var widget = interceptionContext.Entity as Widget;

        if(widget != null)
        {
            //Perform tasks on widget before it is saved...
        }
    }
}

解决方案

In the case of a query, there is no one entity. The executed SQL command can return any number of rows which may result in materialized entities, or some kind of projection. You can access the DbContext or ObjectContext involved, and through its ChangeTracker see the state of the entities within it, but as far as I'm aware there's no direct way to correlate the particular command you are executing with any specific entities. Depending upon your context, the number, type, and state of the entities in the ChangeTracker may be enough to do what you are after.

这篇关于我可以访问实体IDbCommandInterceptor实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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