'CreateDbCommandDefinition in the custom DbProviderServices not called?

I need to intercept the call to the following method of 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). 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. Because I can see the code triggered in this overridden GetService method:

//_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?



Solution 1:[1]

Actually it's very weird to not call the overridden method of CreateDbCommandDefinition which looks likely that it should be called.

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.

Cannot explain this but looks like it's a change from EF5 -> EF6. The code in my question should work in EF5 but looks like it's broken when moved to 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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hopeless