Detailed Description
EntityTypeProjections<TEntity> should automatically apply AsNoTracking() to queries when projecting entities to DTOs. Currently individual projection implementations are responsible for including AsNoTracking(), which requires developers to remember this and can lead to unnecessary tracking overhead when forgotten.
Context
When projecting entities to DTOs EF Core's change tracking is unnecessary overhead since projections are typically used for read-only scenarios, tracking should be disabled by default.
This would:
- Improve performance by default across all projections
- Reduce cognitive load on developers implementing projections
Users who need tracking for specific scenarios, although uncommon, could still call .AsTracking() on the returned IQueryable<TResult>.
Possible Implementation
EntityTypeProjections.Project.cs
protected static IQueryable<TResult> Project<TSource, TResult>(
IEntityTypeProjection<TSource, TResult> entityTypeProjection,
IQueryable<TSource> entityQuery,
ProjectionContext context)
where TSource : class
=> entityTypeProjection.Project(entityQuery.AsNoTracking(), context);
This would include AsNoTracking() into the compiled cached expression delegates.
Detailed Description
EntityTypeProjections<TEntity>should automatically applyAsNoTracking()to queries when projecting entities to DTOs. Currently individual projection implementations are responsible for includingAsNoTracking(), which requires developers to remember this and can lead to unnecessary tracking overhead when forgotten.Context
When projecting entities to DTOs EF Core's change tracking is unnecessary overhead since projections are typically used for read-only scenarios, tracking should be disabled by default.
This would:
Users who need tracking for specific scenarios, although uncommon, could still call
.AsTracking()on the returnedIQueryable<TResult>.Possible Implementation
EntityTypeProjections.Project.csThis would include
AsNoTracking()into the compiled cached expression delegates.