refact update role method

This commit is contained in:
eugeniy_ivanov 2023-02-08 15:12:46 +05:00
parent 54178e8923
commit d35550380c
3 changed files with 14 additions and 8 deletions

View File

@ -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;
}
}

View File

@ -73,6 +73,7 @@ namespace AsbCloudDb.Model
DbSet<TEntity> Set<TEntity>(string name) where TEntity : class;
DbSet<TEntity> Set<TEntity>() where TEntity : class;
EntityEntry<TEntity> Entry<TEntity>([NotNull] TEntity entity) where TEntity : class;
ChangeTracker GetChangeTracker();
}
}

View File

@ -107,7 +107,7 @@ namespace AsbCloudInfrastructure.Repository
var local = dbContext.Set<User>()
.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<int> 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<TEntity[]> 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<TEntity[]> 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;
}