entity-framework-core - 禁用 EF Core 迁移验证查询

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

我正在使用代码优先运行带有 Entity Framework 核心 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 core 中不存在 NullDatabaseInitializer。

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

最佳答案

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

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

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

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

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

关于entity-framework-core - 禁用 EF Core 迁移验证查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53322973/

相关文章:

c# - 我不想直接输入应用程序洞察的 key 来注册日志

azure-application-insights - 忽略错误中的特定 http 状态代码

c# - 无法检索项目元数据。确保这是一个基于MSBuild的.NET Core项目。 (迁移)

c# - 在 ASP.NET 5 中使用自定义 UserStore 和 RoleStore

c# - 无法在 Windows 11 上安装 dotnet-ef 工具

c# - 如何在 ASP.NET 5 Beta 8 控制台应用程序中使用 EF7 并在没有 startup.cs 的情况下连接到 SQL Server?

asp.net - Application Insights 将 ConnectedService.json 文件添加到我的项目中,这有什么作用?

entity-framework-core - 如何防止 EF7 急切地修复导航属性?

Azure ApplicationInsight 和 Power Bi - 直播

docker - dotnet 发布后在 docker 镜像中找不到 Application Insights 依赖项