Skip to content

FEATURE: Automatically apply AsNoTracking() to entity type projections #183

Description

@blelkes-ob

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Fields

    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions