adsense

Sunday, September 8, 2024

EF .Net Core AsNoTracking

EF .Net Core retrieves entities from the database with racking enabled, meaning any changes made to the entity are observed and can be automatically saved back to the database.

This automatic monitory feature is not optimal when it comes to read only operations (SELECT statements). To get more information on Tracking Vs Non Tracking queries read following Microsoft article.

As an optimization for read-only queries we can AsNoTracking as shown below. Adding this will stop E EF Core tracking the results, returning in improved performance as EF Core doesn't manage the state of the entity.

var books= dbContext.Books

    .AsNoTracking()

    .ToList();

 

Cheers,

Samitha

No comments:

Post a Comment