我正在使用代码优先运行带有 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/