简体   繁体   English

自定义 DbProviderServices 中的 CreateDbCommandDefinition 没有被调用?

[英]CreateDbCommandDefinition in the custom DbProviderServices not called?

I need to intercept the call to the following method of DbProviderServices :我需要拦截对DbProviderServices的以下方法的调用:

DbCommandDefinition CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree)

So I need to create a custom DbProviderServices , here it wraps the default SqlProviderServices and forwards the method calls inside (because SqlProviderServices is sealed).所以我需要创建一个自定义的DbProviderServices ,这里它包装了默认的SqlProviderServices并将方法调用转发到内部(因为SqlProviderServices是密封的)。 It looks something like this:它看起来像这样:

public class CustomDbProviderServices : DbProviderServices
{
    static readonly CustomDbProviderServices _instance = new CustomDbProviderServices();        
    static readonly SqlProviderServices _sqlProviderServices = SqlProviderServices.Instance;
    public static CustomDbProviderServices Instance => _instance;

    //other methods ...
}

I can confirm that the custom DbProviderServices is configured to replace the default one OK.我可以确认自定义DbProviderServices已配置为替换默认的 OK。 Because I can see the code triggered in this overridden GetService method:因为我可以看到在这个重写的GetService方法中触发的代码:

//_sqlProviderServices is the wrapped SqlProviderServices
public override object GetService(Type type, object key)
{
   //execution can hit in here
   return _sqlProviderServices.GetService(type, key);
}

But I've never seen a hit inside the following overridden method:但我从未在以下重写方法中看到过成功:

protected override DbCommandDefinition CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree)
{
    //forward call via the wrapped _sqlProviderServices
    ...
}

But somehow the queries run just fine without any error.但不知何故,查询运行得很好,没有任何错误。 So it's strange.所以很奇怪。 I cannot imagine how and why it does not call that method as if it was unused.我无法想象它如何以及为什么不调用该方法,就好像它未被使用一样。 Could you think of any cause for this or it's just a normal behavior?您能想出造成这种情况的任何原因,还是这只是一种正常行为? If it's normal, where could I intercept it to get a reference to the DbCommandTree ?如果正常,我可以在哪里拦截它以获取对DbCommandTree的引用?

Actually it's very weird to not call the overridden method of CreateDbCommandDefinition which looks likely that it should be called.实际上,不调用CreateDbCommandDefinition的重写方法是非常奇怪的,它看起来很可能应该被调用。

By debugging the source code of EF6, I can see that the actual DbProviderServices used by the code is EntityProviderServices , somehow at the time creating the command tree, that provider services is used instead of the custom one of mine.通过调试 EF6 的源代码,我可以看到代码实际使用的DbProviderServicesEntityProviderServices ,不知何故在创建命令树时,使用了提供者服务而不是我的自定义服务。

Cannot explain this but looks like it's a change from EF5 -> EF6.无法解释这一点,但看起来这是从 EF5 -> EF6 的变化。 The code in my question should work in EF5 but looks like it's broken when moved to EF6.我问题中的代码应该可以在 EF5 中使用,但在移至 EF6 时看起来它已损坏。

PS : I know that EF6 is the past and is going to be obsolete soon, but there are still legacy projects to work on and that's my case. PS :我知道 EF6 已成为过去,很快就会过时,但仍有遗留项目需要处理,这就是我的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM