diff --git a/AsbCloudDb/Model/AsbCloudDbContext.cs b/AsbCloudDb/Model/AsbCloudDbContext.cs index afce3792..12805e60 100644 --- a/AsbCloudDb/Model/AsbCloudDbContext.cs +++ b/AsbCloudDb/Model/AsbCloudDbContext.cs @@ -1,5 +1,6 @@ using AsbCloudDb.Model.Subsystems; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.ChangeTracking; using System.Threading; using System.Threading.Tasks; @@ -362,5 +363,7 @@ namespace AsbCloudDb.Model var sql = $"REFRESH MATERIALIZED VIEW {materializedViewName};"; return Database.ExecuteSqlRawAsync(sql, token); } + + public ChangeTracker GetChangeTracker() => ChangeTracker; } } diff --git a/AsbCloudDb/Model/IAsbCloudDbContext.cs b/AsbCloudDb/Model/IAsbCloudDbContext.cs index debd4de6..c2080c72 100644 --- a/AsbCloudDb/Model/IAsbCloudDbContext.cs +++ b/AsbCloudDb/Model/IAsbCloudDbContext.cs @@ -73,6 +73,7 @@ namespace AsbCloudDb.Model DbSet Set(string name) where TEntity : class; DbSet Set() where TEntity : class; EntityEntry Entry([NotNull] TEntity entity) where TEntity : class; + ChangeTracker GetChangeTracker(); } } \ No newline at end of file diff --git a/AsbCloudInfrastructure/Repository/UserRepository.cs b/AsbCloudInfrastructure/Repository/UserRepository.cs index 9148e87b..1668fc6a 100644 --- a/AsbCloudInfrastructure/Repository/UserRepository.cs +++ b/AsbCloudInfrastructure/Repository/UserRepository.cs @@ -107,7 +107,7 @@ namespace AsbCloudInfrastructure.Repository var local = dbContext.Set() .Local .FirstOrDefault(entry => entry.Id.Equals(entity.Id)); - if (local != null) + if (local is not null) { dbContext.Entry(local).State = EntityState.Detached; } @@ -117,8 +117,6 @@ namespace AsbCloudInfrastructure.Repository return entity.Id; } - - public async Task DeleteAsync(int id, CancellationToken token) { var dto = (await GetCacheUserAsync(token)).FirstOrDefault(u => u.Id == id); @@ -221,7 +219,7 @@ namespace AsbCloudInfrastructure.Repository var relations = (await GetCacheRelationUserUserRoleAsync(token)).Where(r => r.IdUser == idUser); dbContext.RelationUserUserRoles.RemoveRange(relations); - if (newRoles?.Any() == true) + if (newRoles?.Any() is true) await dbContext.RelationUserUserRoles.AddRangeAsync(newRoles.Select(role => new RelationUserUserRole { IdUser = idUser, @@ -236,7 +234,8 @@ namespace AsbCloudInfrastructure.Repository where TEntity : class { async Task factory(CancellationToken token) - => await query.AsNoTracking().ToArrayAsync(token); + => await query.ToArrayAsync(token); + dbContext.GetChangeTracker().Clear(); var cache = await GetOrAddCacheAsync(tag, factory, obsolescence, token); return cache.Select(convert); } @@ -244,7 +243,8 @@ namespace AsbCloudInfrastructure.Repository where TEntity : class { async Task factory(CancellationToken token) - => await query.AsNoTracking().ToArrayAsync(token); + => await query.ToArrayAsync(token); + dbContext.GetChangeTracker().Clear(); var cache = await GetOrAddCacheAsync(tag, factory, obsolescence, token); return cache; } @@ -252,7 +252,8 @@ namespace AsbCloudInfrastructure.Repository where TEntity : class { TEntity[] factory() - => query.AsNoTracking().ToArray(); + => query.ToArray(); + dbContext.GetChangeTracker().Clear(); var cache = GetOrAddCache(tag, factory, obsolescence); return cache.Select(convert); } @@ -260,7 +261,8 @@ namespace AsbCloudInfrastructure.Repository where TEntity : class { TEntity[] factory() - => query.AsNoTracking().ToArray(); + => query.ToArray(); + dbContext.GetChangeTracker().Clear(); var cache = GetOrAddCache(tag, factory, obsolescence); return cache; }