Skip to content
This repository has been archived by the owner on Mar 20, 2019. It is now read-only.

Added AsNoTracking for readonly queries #136

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using IdentityServer4.EntityFramework.Interfaces;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

namespace IdentityServer4.EntityFramework.Services
Expand Down Expand Up @@ -44,7 +45,7 @@ public Task<bool> IsOriginAllowedAsync(string origin)
// doing this here and not in the ctor because: https://github.com/aspnet/CORS/issues/105
var dbContext = _context.HttpContext.RequestServices.GetRequiredService<IConfigurationDbContext>();

var origins = dbContext.Clients.SelectMany(x => x.AllowedCorsOrigins.Select(y => y.Origin)).ToList();
var origins = dbContext.Clients.SelectMany(x => x.AllowedCorsOrigins.Select(y => y.Origin)).AsNoTracking().ToList();

var distinctOrigins = origins.Where(x => x != null).Distinct();

Expand Down
1 change: 1 addition & 0 deletions src/IdentityServer4.EntityFramework/Stores/ClientStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public Task<Client> FindClientByIdAsync(string clientId)
.Include(x => x.IdentityProviderRestrictions)
.Include(x => x.AllowedCorsOrigins)
.Include(x => x.Properties)
.AsNoTracking()
.FirstOrDefault(x => x.ClientId == clientId);
var model = client?.ToModel();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public Task StoreAsync(PersistedGrant token)
/// <returns></returns>
public Task<PersistedGrant> GetAsync(string key)
{
var persistedGrant = _context.PersistedGrants.FirstOrDefault(x => x.Key == key);
var persistedGrant = _context.PersistedGrants.AsNoTracking().FirstOrDefault(x => x.Key == key);
var model = persistedGrant?.ToModel();

_logger.LogDebug("{persistedGrantKey} found in database: {persistedGrantKeyFound}", key, model != null);
Expand All @@ -90,7 +90,7 @@ public Task<PersistedGrant> GetAsync(string key)
/// <returns></returns>
public Task<IEnumerable<PersistedGrant>> GetAllAsync(string subjectId)
{
var persistedGrants = _context.PersistedGrants.Where(x => x.SubjectId == subjectId).ToList();
var persistedGrants = _context.PersistedGrants.Where(x => x.SubjectId == subjectId).AsNoTracking().ToList();
var model = persistedGrants.Select(x => x.ToModel());

_logger.LogDebug("{persistedGrantCount} persisted grants found for {subjectId}", persistedGrants.Count, subjectId);
Expand Down
18 changes: 10 additions & 8 deletions src/IdentityServer4.EntityFramework/Stores/ResourceStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ from apiResource in _context.ApiResources
.ThenInclude(s => s.UserClaims)
.Include(x => x.UserClaims);

var api = apis.FirstOrDefault();
var api = apis.AsNoTracking().FirstOrDefault();

if (api != null)
{
Expand All @@ -79,7 +79,7 @@ public Task<IEnumerable<ApiResource>> FindApiResourcesByScopeAsync(IEnumerable<s

var query =
from api in _context.ApiResources
where api.Scopes.Where(x=>names.Contains(x.Name)).Any()
where api.Scopes.Where(x => names.Contains(x.Name)).Any()
select api;

var apis = query
Expand All @@ -88,7 +88,7 @@ where api.Scopes.Where(x=>names.Contains(x.Name)).Any()
.ThenInclude(s => s.UserClaims)
.Include(x => x.UserClaims);

var results = apis.ToArray();
var results = apis.AsNoTracking().ToArray();
var models = results.Select(x => x.ToModel()).ToArray();

_logger.LogDebug("Found {scopes} API scopes in database", models.SelectMany(x => x.Scopes).Select(x => x.Name));
Expand All @@ -113,7 +113,7 @@ where scopes.Contains(identityResource.Name)
var resources = query
.Include(x => x.UserClaims);

var results = resources.ToArray();
var results = resources.AsNoTracking().ToArray();

_logger.LogDebug("Found {scopes} identity scopes in database", results.Select(x => x.Name));

Expand All @@ -127,19 +127,21 @@ where scopes.Contains(identityResource.Name)
public Task<Resources> GetAllResourcesAsync()
{
var identity = _context.IdentityResources
.Include(x => x.UserClaims);
.Include(x => x.UserClaims)
.AsNoTracking();

var apis = _context.ApiResources
.Include(x => x.Secrets)
.Include(x => x.Scopes)
.ThenInclude(s => s.UserClaims)
.Include(x => x.UserClaims);
.ThenInclude(s => s.UserClaims)
.Include(x => x.UserClaims)
.AsNoTracking();

var result = new Resources(
identity.ToArray().Select(x => x.ToModel()).AsEnumerable(),
apis.ToArray().Select(x => x.ToModel()).AsEnumerable());

_logger.LogDebug("Found {scopes} as all scopes in database", result.IdentityResources.Select(x=>x.Name).Union(result.ApiResources.SelectMany(x=>x.Scopes).Select(x=>x.Name)));
_logger.LogDebug("Found {scopes} as all scopes in database", result.IdentityResources.Select(x => x.Name).Union(result.ApiResources.SelectMany(x => x.Scopes).Select(x => x.Name)));

return Task.FromResult(result);
}
Expand Down