MikaelEliasson / EntityFramework.Utilities

Provides extensions for EntityFramework that doesn't exist out of the box like delete and update by query and bulk inserts
443 stars 176 forks source link

Incompatible with MiniProfiler #118

Closed RudeySH closed 6 years ago

RudeySH commented 6 years ago

MiniProfiler wraps the connection to profile it, and now EFUtilities isn't able to recognize the underlying provider.

System.InvalidOperationException: 'No provider supporting the InsertAll operation for this datasource was found'

I'm not sure how EFUtilities detects the datasource. The error message above occurs when DisableDefaultFallback is true, but as far as I understand the source code this boolean is always true. I must be missing something here...

RudeySH commented 6 years ago

I figured out how to make it work. Just wrap existing query providers in a new ProfiledQueryProvider class with the following CanHandle implementation:

public class ProfiledQueryProvider : IQueryProvider
{
    private readonly IQueryProvider _provider;

    public ProfiledQueryProvider(IQueryProvider queryProvider)
    {
        _provider = queryProvider;
    }

    public bool CanHandle(DbConnection storeConnection)
    {
        return _provider.CanHandle((storeConnection as ProfiledDbConnection)?.WrappedConnection ?? storeConnection);
    }

    // Implement other members of IQueryProvider, pass everything to _provider
}

Run this on startup, if you're adding custom query providers you should run this afterwards:

var providers = new IQueryProvider[EntityFramework.Utilities.Configuration.Providers.Count];
EntityFramework.Utilities.Configuration.Providers.CopyTo(providers, 0);
EntityFramework.Utilities.Configuration.Providers.Clear();

for (var i = 0; i < providers.Length; i++)
{
    EntityFramework.Utilities.Configuration.Providers.Add(new ProfiledQueryProvider(providers[i]));
}