【问题标题】:Disable EF Core Migration verification query禁用 EF Core 迁移验证查询
【发布时间】:2018-11-16 21:12:59
【问题描述】:

我正在运行一个带有实体框架核心 2.1.4 的 asp.net core 2.1.4 Web 应用程序,首先使用代码。迁移和种子是在应用程序启动时完成的。

我注意到有几个 EF 查询检查每次调用时的迁移历史记录:

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.

我不想在每次通话时都检查数据库。所以我改变了ServiceLifetime.Singleton。但我仍然在每次通话时看到此验证查询。

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

EF6上有类似问题:How to disable Migration Verification during every DbContext initialization

EF 核心中不存在 NullDatabaseInitializer。

建议做什么?这是正常行为吗?

【问题讨论】:

  • EF Core 的默认行为是什么都不做(相当于 EF6 NullDatabaseInitializer)。一定有一些应用程序代码调用Database.Migrate 或类似代码。
  • 没错,我在应用程序启动时自己打电话给Database.Migrate()。但是验证查询是针对每个新的 http 请求进行的。
  • 不。必须有一些您的代码(或您使用的 EF Core 以外的库)正在执行此操作。 EF Core 没有。
  • 感谢您的澄清!也许Microsoft.AspNetCore.IdentityIdentityServer4 导致了这个问题。 services.AddIdentity&lt;ApplicationUser, ApplicationRole&gt;() .AddEntityFrameworkStores&lt;ApplicationDbContext&gt;() .AddDefaultTokenProviders();

标签: entity-framework-core azure-application-insights


【解决方案1】:

我发现了问题: https://github.com/Microsoft/ApplicationInsights-aspnetcore/issues/778

使用“真正的”SQL 分析器后,我发现它像@Ivan-Stoev 所说的那样按预期工作。我这边的请求不知何故使用了相同的 operation_id。因此,应用程序洞察向我显示的跟踪和依赖关系实际上并不相关。

我通过删除 AI 的默认 DependecyTracking 来修复它。

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

感谢您的时间,很抱歉浪费了它..

【讨论】:

    猜你喜欢
    • 2021-11-23
    • 2022-11-11
    • 2023-01-26
    • 2021-03-21
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多