𝗨𝘀𝗲 𝗔𝘀𝗡𝗼𝗧𝗿𝗮𝗰𝗸𝗶𝗻𝗴() 𝗳𝗼𝗿 𝗥𝗲𝗮𝗱-𝗢𝗻𝗹𝘆 𝗤𝘂𝗲𝗿𝗶𝗲𝘀 𝗶𝗻 𝗘𝗙 𝗖𝗼𝗿𝗲

 𝗨𝘀𝗲 𝗔𝘀𝗡𝗼𝗧𝗿𝗮𝗰𝗸𝗶𝗻𝗴() 𝗳𝗼𝗿 𝗥𝗲𝗮𝗱-𝗢𝗻𝗹𝘆 𝗤𝘂𝗲𝗿𝗶𝗲𝘀 𝗶𝗻 𝗘𝗙 𝗖𝗼𝗿𝗲

By default, EF Core tracks all queried entities, even when you don’t intend to modify them.

That’s unnecessary overhead in common read-only scenarios like:

 • Displaying data

 • Listing records in APIs

 • Generating reports or exports


𝗪𝗵𝘆 𝗶𝘁 𝗺𝗮𝘁𝘁𝗲𝗿𝘀:

- Faster query performance

- Lower memory usage

- No change tracking = no accidental updates

𝗘𝘅𝗮𝗺𝗽𝗹𝗲

var orders = await context.Orders

  .AsNoTracking()

  .Include(o => o.Customer)

  .ToListAsync()

You’ll notice improved speed and reduced memory footprint, especially with large datasets.

𝗪𝗵𝗲𝗻 𝘁𝗼 𝗨𝘀𝗲 𝗔𝘀𝗡𝗼𝗧𝗿𝗮𝗰𝗸𝗶𝗻𝗴()

 • Use it for any query where you don’t plan to modify the returned entities, such as:

 • Read-only API endpoints

 • Reports, exports, dashboards

 • Background jobs doing data reads

 • Large data retrieval operations

 • Projections into DTOs (e.g., using .Select(...))

𝗪𝗵𝗲𝗻 𝗡𝗼𝘁 𝘁𝗼 𝗨𝘀𝗲 𝗔𝘀𝗡𝗼𝗧𝗿𝗮𝗰𝗸𝗶𝗻𝗴()

 • Avoid it when you intend to update the entities, including:

 • Editing or saving changes to the queried entities

 • Loading related data that you plan to modify

 • Using change tracking features like:

   - Automatic fix-up

   - DetectChanges()

   - Change detection during SaveChanges

 • Stateful workflows requiring tracked entities


Comments

Popular posts from this blog

Performance Optimization in Sitecore

Strategies for Migrating to Sitecore from legacy or upgrading from older Sitecore

Azure Event Grid Sample code