Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does .Include method hit the database in LINQ?

I am testing the Linq query below. And I'm checking the log to see if there is a database hit:

var productEntities = _context.Set<Product>()
                .Include(p => p.OrderItems)
                .Include(p => p.OrderItems.Select(oi => oi.Order))
                .Include(p => p.OrderItems.Select(oi => oi.Order.Client));

            Console.WriteLine(productEntities.Count());

Whether I comment out the call to Count() or not, the log shows this same output:

Opened connection at 8/20/2016 3:32:53 PM -04:00

SELECT Count(*) FROM INFORMATION_SCHEMA.TABLES AS t WHERE t.TABLE_SCHEMA + '.' + t.TABLE_NAME IN ('dbo.AccountRecords','dbo.Products','dbo.Doughs','dbo.OrderItems','dbo.Orders','dbo.Clients','dbo.ShippingInformations','dbo.OrderSpecialClients','dbo.OrderSpecialProducts','dbo.Taxes','dbo.FixedOrders','dbo.FixedOrderItems','dbo.OrderSubstitutions','dbo.OrderPerformanceLog') OR t.TABLE_NAME = 'EdmMetadata' -- Executing at 8/20/2016 3:32:53 PM -04:00 -- Completed in 2 ms with result: 14

Closed connection at 8/20/2016 3:32:53 PM -04:00 Opened connection at 8/20/2016 3:32:53 PM -04:00 SELECT [GroupBy1].[A1] AS [C1] FROM ( SELECT COUNT(1) AS [A1] FROM [dbo].[__MigrationHistory] AS [Extent1] WHERE [Extent1].[ContextKey] = @p__linq__0 ) AS [GroupBy1] -- p__linq__0: 'Arhoma.Core.Data.ArhomaContext' (Type = String, Size = 4000) -- Executing at 8/20/2016 3:32:53 PM -04:00 -- Failed in 51 ms with error: Invalid object name 'dbo.__MigrationHistory'.

Closed connection at 8/20/2016 3:32:53 PM -04:00 Opened connection at 8/20/2016 3:32:53 PM -04:00 SELECT [GroupBy1].[A1] AS [C1] FROM ( SELECT COUNT(1) AS [A1] FROM [dbo].[__MigrationHistory] AS [Extent1] ) AS [GroupBy1] -- Executing at 8/20/2016 3:32:53 PM -04:00 -- Failed in 36 ms with error: Invalid object name 'dbo.__MigrationHistory'.

Closed connection at 8/20/2016 3:32:53 PM -04:00 Opened connection at 8/20/2016 3:32:53 PM -04:00 SELECT [GroupBy1].[A1] AS [C1] FROM ( SELECT COUNT(1) AS [A1] FROM [dbo].[Products] AS [Extent1] ) AS [GroupBy1] -- Executing at 8/20/2016 3:32:53 PM -04:00 -- Completed in 0 ms with result: SqlDataReader

Closed connection at 8/20/2016 3:32:53 PM -04:00 299

My question: Why does it hit the database? I thought the hit is made when I add a .ToList() or something or when it calls a .Count()? But it still hits the database even when I remove the call to Count(). So now I'm thinking, is it the .Include that forces a database hit?

like image 523
Ray Avatar asked Nov 28 '25 15:11

Ray


1 Answers

All these MigrationHistory related queries run only once, the very first time your code connects to the database. The EF just tries to verify if the database needs to be upgraded to a newer version.

To confirm that, try any meaningful query before the one you presented so that the query with Includes is the second one. You will see all this versioning noise, then your first query and then nothing, as Include itself is not retrieving anything.

like image 91
Wiktor Zychla Avatar answered Dec 01 '25 04:12

Wiktor Zychla