EF AsNoTracking
• Normally, when you pull data from a DbSet with Entity Framework, EF adds that data to a mechanism called Change Tracker.
Example
• That is, a copy of each entity loaded into memory (for example, your PRODUCTS records) is kept, and if you make changes to that entity, EF keeps track of which fields have changed.
• When you later call SaveChanges(), EF automatically applies these changes to the database.
• If you are only going to retrieve data for listing/reporting/display, that is, if you are not going to make any changes and save them, this extra tracking by Change Tracker creates unnecessary performance costs.
var products= await context.PRODUCTS.AsNoTracking()
.Where(u => !string.IsNullOrEmpty(u.PRODUCTKOD) && !string.IsNullOrEmpty(u.PRODUCTNAME))
.ToListAsync();
Result
• When you use AsNoTracking(), EF doesn't track these entities; it treats them as read-only. This means that even if you make changes to objects in the product list, they won't be reflected in the database when SaveChanges() is called.
• AsNoTracking() improves performance because EF does not keep a record of change trackers for each entity.
• It uses less memory.