This video is available to students only

Fixing Some Warnings

In this lesson, we're going to fix some warnings

Fixing errors#

We have done pretty well with the API so far. However, you might notice some warnings in the way if we add more than two navigation properties in the include statement. Let's open the CoursesWithCategoriesSpecification and add another statement to include the category and if we run get course request with an id and look at the console, you would see a warning which says 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. This simply means that Entity Framework Core is using a single query by default, which means that when we have lots of navigation properties, we have too many inner joins in the sql query which are causing slow performance.

We can either ignore this warning by writing a piece of code, or we can change the behavior in which the query is being made. I would choose the latter. Let's go to our Startup class, and inside Configure services method, then inside services.AddDbContext, we will configure our query splitting behavior. Let's put a comma and write x.UseQuerySplittingBehavior and inside, we will pass QuerySplittingBehavior.SplitQuery. By default, it is using the Single Behavior. Now, rather than making a single query with all the inner joins for adding navigation properties, it will split the query into one or more parts so that it doesn't impact the performance.

API/Startup.cs

Start a new discussion. All notification go to the author.