Disable EF Core Migration verification query

倾然丶 夕夏残阳落幕 提交于 2021-02-05 08:18:06

问题


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:


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..



来源:https://stackoverflow.com/questions/53322973/disable-ef-core-migration-verification-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!