Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use AsNoTracking on Readonly queries #221

Closed
jaredcnance opened this issue Jan 19, 2018 · 2 comments
Closed

Use AsNoTracking on Readonly queries #221

jaredcnance opened this issue Jan 19, 2018 · 2 comments
Milestone

Comments

@jaredcnance
Copy link
Contributor

https://docs.microsoft.com/en-us/ef/core/querying/tracking

@bart-degreed
Copy link
Contributor

Recent changes in EF Core have resulted in AsNoTracking() being faster for simple queries, but slower for complex queries (described at dotnet/efcore#14366).

@bart-degreed bart-degreed added this to the Future milestone Sep 22, 2020
@bart-degreed
Copy link
Contributor

As noted above, it depends on use cases whether or not it results in improvements, so I don't think we should have this built-in.

Nevertheless, it can easily be added by library users themselves:

public class NoTrackingRepository<TResource, TId> : EntityFrameworkCoreRepository<TResource, TId>
    where TResource : class, IIdentifiable<TId>
{
    private readonly IJsonApiRequest _request;

    public NoTrackingRepository(IJsonApiRequest request, ITargetedFields targetedFields,
        IDbContextResolver contextResolver, IResourceGraph resourceGraph, IResourceFactory resourceFactory,
        IEnumerable<IQueryConstraintProvider> constraintProviders, ILoggerFactory loggerFactory)
        : base(targetedFields, contextResolver, resourceGraph, resourceFactory, constraintProviders, loggerFactory)
    {
        _request = request;
    }

    protected override IQueryable<TResource> GetAll()
    {
        if (_request.IsReadOnly)
        {
            return base.GetAll().AsNoTracking();
        }

        return base.GetAll();
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants