Поэтому я добавил такой собственный перехватчик:
Код: Выделить всё
public class QueryHintInterceptor : DbCommandInterceptor
{
private DbCommand GetCommandWithQueryHint(DbCommand command)
{
if (command.CommandText.StartsWith($"-- {Const.TagWith.OptimizeForUnknown}") && !command.CommandText.EndsWith(Const.TagWith.OptimizeForUnknown))
command.CommandText += $" {Const.TagWith.OptimizeForUnknown}";
return command;
}
public override InterceptionResult ReaderExecuting(DbCommand command, CommandEventData eventData, InterceptionResult result)
{
return base.ReaderExecuting(GetCommandWithQueryHint(command), eventData, result);
}
public override ValueTask ReaderExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult result, CancellationToken cancellationToken = default)
{
return base.ReaderExecutingAsync(GetCommandWithQueryHint(command), eventData, result, cancellationToken);
}
}
Код: Выделить всё
public static IQueryable OptimizeForUnknown(this IQueryable dbset) where TEntity : ImportEntity
{
return dbset.TagWith(Const.TagWith.OptimizeForUnknown); //we set this tag and detect it on execution to inject the desired sql
}
Код: Выделить всё
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.AddInterceptors(new QueryHintInterceptor());
}
}
Код: Выделить всё
var query = icrmContext.Set()
.AsNoTracking()
.OptimizeForUnknown();
Код: Выделить всё
{
"status": "Unhealthy",
"results": {
"MyDbContext": {
"status": "Unhealthy",
"description": "'OnConfiguring' cannot be used to modify DbContextOptions when DbContext pooling is enabled.",
"responseTime": "861,8354",
"data": {}
}
}
}
Код: Выделить всё
services.AddDbContextPool((sp, options) =>
{
options
.UseSqlServer(sqlConnectionString, opts =>
{
opts.CommandTimeout(commandTimeoutInSeconds);
opts.EnableRetryOnFailure();
})
.AddInterceptors(ServiceProviderExtensions.GetRequiredService(sp));
});
Вот моя проверка работоспособности -конфигурация:
Код: Выделить всё
var healthChecksBuilder = services.AddHealthChecks()
.AddDbContextCheck();
Подробнее здесь: https://stackoverflow.com/questions/790 ... althchecks
Мобильная версия