Home > Net >  Disable EF Core "QuerySplittingBehavior.SingleQuery" warning
Disable EF Core "QuerySplittingBehavior.SingleQuery" warning

Time:11-03

I use a lot of multiple Include on EF entities in my aspnet core project. I now have prod log littered with those:

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))' 

I would like to disable either the warning all together or filter it before it gets logged.

I use nlog.web and the logging is configured in appSettings.json

  "Logging": {
    "LogLevel": {
      "Default": "Trace",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information",
      "Microsoft.EntityFrameworkCore": "Warning",
      "IdentityServer4": "Information"
    }

CodePudding user response:

You can disable the warning for that message by configuring your DbContext. In the OnConfiguring method, add this line:

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

However, I would suggest that fixing the queries that are causing this in the first place - these warnings exist for a good reason.

  • Related