Skip to content
Advertisement

Entity Framework Core 3.0 .Include call taking 10x more time

I reviewed almost everything online but couldn’t get it work in a reasonable time.

Entity Framework Core 3.0 .Include call is taking 10x more time than calling the individual tables separately and then mapping them in C# code.

I have two tables product and productimages, where product productimages is a collection inside product.

Now when I chain the call with .Include function, it takes forever to get the records form DB:

products = _context.ProductUploadQueue
               .Include(x => x.ProductUploadQueueImage)
               .Where(x => x.ClientId == clientId && x.PalletId == palletID)
               .ToList();

but if I do the same while calling the both tables individually and not chaining the Include call, it speeds up everything:

 var g = _context.ProductUploadQueue
      .AsNoTracking()
      .Where(x => x.ClientId == clientId && x.PalletId == palletID)
      .ToList(); 

 var hp = g.Select(x => x.Id); 

 var y = _context.ProductUploadQueueImage
      .Where(x => hp.Contains(x.ProductUploadQueueId))  
      .ToList();

How can I speed it up?

Advertisement

Answer

how can I speed it up?

You just discovered how. Go back and look at g and you’ll find that all of the productUploadQueueImage navigation properties have been populated. When you run the second query the Change Tracker will fix-up any relationships as it loads the second colleciotn.

Before EF Core 3, Include queries could be executed in multiple round-trips, like you are doing here. Generating a single query that includes multiple tables is often significantly slower.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement