Stack Undefined
Stack Undefined

Reputation: 1330

EF Core 5.0 still tracks entities even after adding AsNoTrackingWithIdentityResolution()

EF Core 5.0.1 and .NET 5

I have a query in which the entities don't need to be tracked since the result is for a HttpGet endpoint.

return await _context.Customers.AsNoTracking()
       .Include(x => x.Orders)
       .Include(x => x.OrderDetails)
       .Include(x => x.Payments)
       .Include(x => x.Shipments)
       .Where(x => x.Id == CustomerDto.Id)
       .ToListAsync();

I'm not seeing anything in the debug log indicating entities are being tracked in the above query. But if I replace the AsNoTracking() with AsNoTrackingWithIdentityResolution(), I can see hundreds of lines similar to Order entities are being tracked. Make sure not to log sensitive information, OrderDetail entities are being tracked. Make sure not to log sensitive information, ...in the log. The documentation states EF Core uses a standalone tracking internally so not sure that's what I'm seeing. https://learn.microsoft.com/en-us/ef/core/querying/tracking

When you configure the query to use identity resolution with no tracking, we use a stand-alone change tracker in the background when generating query results so each instance is materialized only once.

There is a good chance I'm also dealing with the Cartesian explosion but not concerned about it at this point.

Also there is not much documentation on where to place the AsNoTracking() or AsNoTrackingWithIdentityResolution() in a query. Does each table being queried need the no tracking call or one is enough for the entire query? Placing it right before ToList() like this .AsNoTracking().ToListAsync() doesn't work - entities are still tracked.

Upvotes: 1

Views: 175

Answers (0)

Related Questions