Disable EF Core Migration verification query

azure-application-insights entity-framework-core

Question

I'm running an asp.net core 2.1.4 web application with entity framework core 2.1.4 using code first. The migration and seed are done on application startup.

I've noticed a couple of EF queries checking for checking the migration history on every call:

Executed DbCommand (47ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] SELECT OBJECT_ID(N'[__EFMigrationsHistory]');

Executed DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] SELECT OBJECT_ID(N'[__EFMigrationsHistory]');

Executed DbCommand (4ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] SELECT [MigrationId], [ProductVersion] FROM [__EFMigrationsHistory] ORDER BY [MigrationId];

No migrations were applied. The database is already up to date.

I don't want to check the database on every call. So I changed the ServiceLifetime.Singleton. But still I see this verification queries on every call.

services.AddDbContext<ApplicationDbContext>(options =>
{
    options.UseSqlServer(dbConnectionString,
        builder => builder.MigrationsAssembly(migrationsAssembly));
}, ServiceLifetime.Singleton, ServiceLifetime.Singleton);

There is a similar question on EF6: How to disable Migration Verification during every DbContext initialization

The NullDatabaseInitializer doesn't exist in EF core.

What is recommended to do? Is this normal behavior?

1
1
11/16/2018 1:12:59 PM

Accepted Answer

I found the issue: https://github.com/Microsoft/ApplicationInsights-aspnetcore/issues/778

After using a "real" SQL profiler I found out it was working as expected like @Ivan-Stoev said. The requests on my side were somehow using the same operation_id. So the traces and dependencies that where shown to me by application insights where actually not related.

I fixed it by removing the default DependecyTracking of AI.

private void RemoveDefaultAiDependencyTracking(IServiceCollection services)
{
    var serviceDescriptor = services.FirstOrDefault(descriptor => descriptor.ImplementationType == typeof(DependencyTrackingTelemetryModule));
    services.Remove(serviceDescriptor);
}

Thanks for you time and sorry for wasting it..

2
11/16/2018 1:11:45 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow