禁用 EF Core "QuerySplittingBehavior.SingleQuery" 警告

Disable EF Core "QuerySplittingBehavior.SingleQuery" warning

我在我的 aspnet 核心项目中对 EF 实体使用了很多多个 Include。 我现在的 prod 日志中到处都是:

13:49:54.0149751|Warn|Microsoft.EntityFrameworkCore.Query|Compiling a
  query which loads related collections for more than one collection
  navigation either via 'Include' or through projection but no 
  'QuerySplittingBehavior' has been configured. By default Entity 
  Framework will use 'QuerySplittingBehavior.SingleQuery' which can 
  potentially result in slow query performance. See 
  https://go.microsoft.com/fwlink/?linkid=2134277 for more information. 
  To identify the query that's triggering this warning call 
  'ConfigureWarnings(w => 
    w.Throw(RelationalEventId.MultipleCollectionIncludeWarning))' 

我想一起禁用警告或在它被记录之前过滤它。

我使用 nlog.web 并且在 appSettings.json

中配置了日志记录
  "Logging": {
    "LogLevel": {
      "Default": "Trace",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information",
      "Microsoft.EntityFrameworkCore": "Warning",
      "IdentityServer4": "Information"
    }

您可以通过配置 DbContext 来禁用该消息的警告。在 OnConfiguring 方法中,添加这一行:

optionsBuilder
    .ConfigureWarnings(x => x.Ignore(RelationalEventId.MultipleCollectionIncludeWarning));

但是,我建议首先修复导致此问题的查询 - 这些警告的存在是有充分理由的。