CS2-50: Added .AsNoTracking() to services' queries.

This commit is contained in:
KharchenkoVV 2021-08-11 10:16:01 +05:00
parent 814f809c3e
commit 744266336e
12 changed files with 43 additions and 26 deletions

View File

@ -482,7 +482,7 @@ namespace AsbCloudDb.Model
{
From = g.Min(d => d.Date),
To = g.Max(d => d.Date)
}).FirstOrDefault();
}).AsNoTracking().FirstOrDefault();
if (datesRange is null)
return (DateTime.MinValue, DateTime.MaxValue);

View File

@ -57,7 +57,7 @@ namespace AsbCloudInfrastructure.Services
WellDepth = d.WellDepth ?? 0.0,
BitDepth = d.BitDepth ?? 0.0,
Date = d.Date
}).ToList();
}).AsNoTracking().ToList();
}
public IEnumerable<WellDepthToIntervalDto> GetWellDepthToInterval(int idWell,
@ -125,7 +125,7 @@ namespace AsbCloudInfrastructure.Services
if (skip > 0)
operations = operations.Skip(skip);
var operationsList = operations.Take(take).ToList();
var operationsList = operations.Take(take).AsNoTracking().ToList();
if (operationsList.Count == 0)
return result;
@ -169,7 +169,7 @@ namespace AsbCloudInfrastructure.Services
OperationName = g.Key.Name,
Duration = g.Where(g => g.DurationSec > 0)
.Sum(a => a.DurationSec)
}).ToList();
}).AsNoTracking().ToList();
return operations;
}
@ -200,7 +200,7 @@ namespace AsbCloudInfrastructure.Services
IntervalStart = g.Min(d => d.UnixDate),
OperationName = g.Key.Name,
OperationsDuration = g.Sum(an => an.DurationSec)
}).ToList();
}).AsNoTracking().ToList();
var operationsGroupedByInterval = operations.GroupBy(op => op.IntervalStart)
.Select(o => new TelemetryOperationInfoDto

View File

@ -9,6 +9,7 @@ using System.Linq;
using System.Security.Claims;
using System.Security.Cryptography;
using System.Text;
using Microsoft.EntityFrameworkCore;
namespace AsbCloudInfrastructure.Services
{
@ -97,7 +98,7 @@ namespace AsbCloudInfrastructure.Services
public int ChangePassword(string userLogin, string newPassword)
{
var user = db.Users.FirstOrDefault(u => u.Login == userLogin);
var user = db.Users.AsNoTracking().FirstOrDefault(u => u.Login == userLogin);
if (user == null)
return -1;
@ -138,6 +139,7 @@ namespace AsbCloudInfrastructure.Services
{
var user = db
.GetUsersByLogin(login)
.AsNoTracking()
.FirstOrDefault();
if (user is null)

View File

@ -24,7 +24,7 @@ namespace AsbCloudInfrastructure.Services
.Include(w => w.Cluster)
.ThenInclude(c => c.Deposit)
where well.RelationCompaniesWells.Any(r => r.IdCompany == idCompany)
select well).ToList();
select well).AsNoTracking().ToList();
var gDepositEntities = wellEntities
.GroupBy(w => w.Cluster)
@ -65,6 +65,7 @@ namespace AsbCloudInfrastructure.Services
var entities = db.GetWellsForCompany(idCompany)
.Select(e => e.Cluster)
.Distinct()
.AsNoTracking()
.ToList();
var dtos = entities.Select(e => new ClusterDto
@ -84,6 +85,7 @@ namespace AsbCloudInfrastructure.Services
.Select(e => e.Cluster)
.Where(e => e.IdDeposit == depositId)
.Distinct()
.AsNoTracking()
.ToList();
var dtos = entities.Select(e => new ClusterDto
@ -101,6 +103,7 @@ namespace AsbCloudInfrastructure.Services
{
var entities = db.GetWellsForCompany(idCompany)
.Where(e => e.IdCluster == idCluster)
.AsNoTracking()
.ToList();
var dtos = entities.Select(e => new WellDto
@ -164,12 +167,12 @@ namespace AsbCloudInfrastructure.Services
CompanyType = c.Company.CompanyType.Caption,
}),
WellType = e.WellType.Caption,
}).ToList();
}).AsNoTracking().ToList();
if (!wellStatDtos.Any())
return null;
var clusterById = db.Clusters.FirstOrDefault(c => c.Id == idCluster);
var clusterById = db.Clusters.AsNoTracking().FirstOrDefault(c => c.Id == idCluster);
return new ClusterStatDto
{

View File

@ -24,7 +24,8 @@ namespace AsbCloudInfrastructure.Services
public virtual async Task<Tdto> GetAsync(int id, CancellationToken token = default)
{
var entity = await dbSet.FirstOrDefaultAsync(e => e.Id == id, token).ConfigureAwait(false);
var entity = await dbSet.AsNoTracking()
.FirstOrDefaultAsync(e => e.Id == id, token).ConfigureAwait(false);
var dto = entity.Adapt<Tdto>();
return dto;
}
@ -59,7 +60,8 @@ namespace AsbCloudInfrastructure.Services
public virtual Task<int> DeleteAsync(int id, CancellationToken token = default)
{
var entity = dbSet.FirstOrDefault(e => e.Id == id);
var entity = dbSet.AsNoTracking()
.FirstOrDefault(e => e.Id == id);
if (entity == default)
return Task.FromResult(0);
dbSet.Remove(entity);
@ -68,7 +70,7 @@ namespace AsbCloudInfrastructure.Services
public virtual Task<int> DeleteAsync(IEnumerable<int> ids, CancellationToken token = default)
{
var entities = dbSet.Where(e => ids.Contains(e.Id));
var entities = dbSet.Where(e => ids.Contains(e.Id)).AsNoTracking();
if (entities == default)
return Task.FromResult(0);
dbSet.RemoveRange(entities);

View File

@ -1,6 +1,7 @@
using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using Microsoft.EntityFrameworkCore;
using AsbCloudInfrastructure.Services.Cache;
using Mapster;
using System;
@ -59,7 +60,7 @@ namespace AsbCloudInfrastructure.Services
query = query.Where(d => d.Id % m == 0);
}
var entities = query.ToList();
var entities = query.AsNoTracking().ToList();
var dtos = entities.Adapt<DataSaubBaseDto>();
@ -79,7 +80,7 @@ namespace AsbCloudInfrastructure.Services
where d.IdTelemetry == telemetryId
&& d.Date > dtoMinDate
&& d.Date < dtoMaxDate
select d).ToList();
select d).AsNoTracking().ToList();
if (oldDataSaubBase.Any())
db.DataSaubBases.RemoveRange(oldDataSaubBase);

View File

@ -49,7 +49,7 @@ namespace AsbCloudInfrastructure.Services
{
var filesInfoQuery = db.Files.Include(f => f.User)
.Where(f => f.IdWell == idWell &&
f.IdCategory == idCategory);
f.IdCategory == idCategory).AsNoTracking();
if (!filesInfoQuery.Any())
return null;
@ -57,15 +57,15 @@ namespace AsbCloudInfrastructure.Services
var result = new PaginationContainer<FilePropertiesDto>() { Skip = skip, Take = take };
if (begin != default)
filesInfoQuery = filesInfoQuery.Where(m => m.Date >= begin);
filesInfoQuery = filesInfoQuery.Where(m => m.Date >= begin).AsNoTracking();
if (end != default)
filesInfoQuery = filesInfoQuery.Where(m => m.Date <= end);
filesInfoQuery = filesInfoQuery.Where(m => m.Date <= end).AsNoTracking();
result.Count = filesInfoQuery.Count();
if (skip > 0)
filesInfoQuery = filesInfoQuery.Skip(skip);
filesInfoQuery = filesInfoQuery.Skip(skip).AsNoTracking();
var filesInfoList = filesInfoQuery.OrderBy(f => f.Date).Take(take).ToList();
@ -91,7 +91,7 @@ namespace AsbCloudInfrastructure.Services
public (int Id, string Name, int IdCategory)? GetFileInfo(int fileId)
{
var fileInfo = db.Files.FirstOrDefault(f => f.Id == fileId);
var fileInfo = db.Files.AsNoTracking().FirstOrDefault(f => f.Id == fileId);
if (fileInfo is null)
return null;

View File

@ -3,6 +3,7 @@ using AsbCloudDb.Model;
using Mapster;
using System.Linq;
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
namespace AsbCloudInfrastructure.Services
{
@ -17,7 +18,7 @@ namespace AsbCloudInfrastructure.Services
public Tdto Get(int idWell, int idCategory)
{
var entity = db.LastData.FirstOrDefault(e =>
var entity = db.LastData.AsNoTracking().FirstOrDefault(e =>
e.IdWell == idWell && e.IdCategory == idCategory);
if (entity is null)
@ -31,7 +32,8 @@ namespace AsbCloudInfrastructure.Services
{
var model = value.Adapt<TModel>();
var entity = db.LastData.FirstOrDefault(ld => ld.IdWell == idWell &&
var entity = db.LastData.AsNoTracking()
.FirstOrDefault(ld => ld.IdWell == idWell &&
ld.IdCategory == idCategory);
if (entity is not null)

View File

@ -79,7 +79,7 @@ namespace AsbCloudInfrastructure.Services
if (skip > 0)
query = query.Skip(skip);
var messagesList = query.Take(take).ToList();
var messagesList = query.Take(take).AsNoTracking().ToList();
if (messagesList.Count == 0)
return result;

View File

@ -29,7 +29,8 @@ namespace AsbCloudInfrastructure.Services
this.telemetryService = telemetryService;
this.fileService = fileService;
this.queue = queue;
ReportCategoryId = db.FileCategories.FirstOrDefault(c =>
ReportCategoryId = db.FileCategories.AsNoTracking()
.FirstOrDefault(c =>
c.Name.Equals("Рапорт")).Id;
}
@ -121,12 +122,15 @@ namespace AsbCloudInfrastructure.Services
select d.Date).Union(
from m in db.TelemetryMessages
where m.IdTelemetry == telemetryId
select m.Date).DefaultIfEmpty().GroupBy(g => true)
select m.Date).DefaultIfEmpty()
.GroupBy(g => true)
.AsNoTracking()
.Select(g => new
{
From = g.Min(),
To = g.Max()
}).OrderBy(gr => gr.From).FirstOrDefault();
}).OrderBy(gr => gr.From)
.FirstOrDefault();
return new DatesRangeDto
{
@ -144,6 +148,7 @@ namespace AsbCloudInfrastructure.Services
&& r.Step <= stepSeconds
&& r.Format == format
select r).OrderBy(o => o.File.Date)
.AsNoTracking()
.Take(512).ToList();
return suitableReportsNames;

View File

@ -4,7 +4,6 @@ using AsbCloudDb.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mapster;
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;

View File

@ -6,6 +6,7 @@ using Mapster;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
namespace AsbCloudInfrastructure.Services
{
@ -30,6 +31,7 @@ namespace AsbCloudInfrastructure.Services
{
wells = db.GetWellsForCompany(idCompany)
.Where(w => activeTelemetriesUids.Contains(w.Telemetry.RemoteUid))
.AsNoTracking()
.ToList();
}
return wells.Select(w => From(w));
@ -49,6 +51,7 @@ namespace AsbCloudInfrastructure.Services
var entities = db
.WellOperations
.Where(o => o.IdWell == idWell)
.AsNoTracking()
.ToList();
var dtos = entities.Adapt<WellOperationDto>();