Skip to content

Commit

Permalink
Add AsNoTracking to read-only queries.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Jul 26, 2023
1 parent cb220f2 commit 7a8809c
Showing 1 changed file with 82 additions and 27 deletions.
109 changes: 82 additions & 27 deletions Server/Services/DataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,9 @@ public async Task<Result> AddOrUpdateSavedScript(SavedScript script, string user
}

dbContext.SavedScripts.Update(script);
script.CreatorId = userId;
script.CreatorId = user.Id;
script.Creator = user;
script.OrganizationID = user.Id;
script.OrganizationID = user.OrganizationID;
await dbContext.SaveChangesAsync();
return Result.Ok();
}
Expand Down Expand Up @@ -1062,6 +1062,7 @@ public async Task<Result<Alert>> GetAlert(string alertId)
using var dbContext = _appDbFactory.GetContext();

var alert = await dbContext.Alerts
.AsNoTracking()
.Include(x => x.Device)
.Include(x => x.User)
.FirstOrDefaultAsync(x => x.ID == alertId);
Expand All @@ -1079,6 +1080,7 @@ public Alert[] GetAlerts(string userId)
using var dbContext = _appDbFactory.GetContext();

return dbContext.Alerts
.AsNoTracking()
.Include(x => x.Device)
.Include(x => x.User)
.Where(x => x.UserID == userId)
Expand All @@ -1098,6 +1100,7 @@ public ApiToken[] GetAllApiTokens(string userId)
}

return dbContext.ApiTokens
.AsNoTracking()
.Where(x => x.OrganizationID == user.OrganizationID)
.OrderByDescending(x => x.LastUsed)
.ToArray();
Expand All @@ -1108,6 +1111,7 @@ public ScriptResult[] GetAllCommandResults(string orgId)
using var dbContext = _appDbFactory.GetContext();

return dbContext.ScriptResults
.AsNoTracking()
.Where(x => x.OrganizationID == orgId)
.OrderByDescending(x => x.TimeStamp)
.ToArray();
Expand All @@ -1118,6 +1122,7 @@ public ScriptResult[] GetAllCommandResultsForUser(string orgId, string userName,
using var dbContext = _appDbFactory.GetContext();

return dbContext.ScriptResults
.AsNoTracking()
.Where(x => x.OrganizationID == orgId &&
x.SenderUserName == userName &&
x.DeviceID == deviceId)
Expand All @@ -1129,14 +1134,18 @@ public Device[] GetAllDevices(string orgId)
{
using var dbContext = _appDbFactory.GetContext();

return dbContext.Devices.Where(x => x.OrganizationID == orgId).ToArray();
return dbContext.Devices
.AsNoTracking()
.Where(x => x.OrganizationID == orgId)
.ToArray();
}

public InviteLink[] GetAllInviteLinks(string organizationId)
{
using var dbContext = _appDbFactory.GetContext();

return dbContext.InviteLinks
.AsNoTracking()
.Where(x => x.OrganizationID == organizationId)
.ToArray();
}
Expand All @@ -1146,6 +1155,7 @@ public ScriptResult[] GetAllScriptResults(string orgId, string deviceId)
using var dbContext = _appDbFactory.GetContext();

return dbContext.ScriptResults
.AsNoTracking()
.Where(x => x.OrganizationID == orgId && x.DeviceID == deviceId)
.OrderByDescending(x => x.TimeStamp)
.ToArray();
Expand All @@ -1156,6 +1166,7 @@ public ScriptResult[] GetAllScriptResultsForUser(string orgId, string userName)
using var dbContext = _appDbFactory.GetContext();

return dbContext.ScriptResults
.AsNoTracking()
.Where(x => x.OrganizationID == orgId && x.SenderUserName == userName)
.OrderByDescending(x => x.TimeStamp)
.ToArray();
Expand All @@ -1165,7 +1176,9 @@ public RemotelyUser[] GetAllUsersForServer()
{
using var dbContext = _appDbFactory.GetContext();

return dbContext.Users.ToArray();
return dbContext.Users
.AsNoTracking()
.ToArray();
}

public async Task<RemotelyUser[]> GetAllUsersInOrganization(string orgId)
Expand All @@ -1178,6 +1191,7 @@ public async Task<RemotelyUser[]> GetAllUsersInOrganization(string orgId)
using var dbContext = _appDbFactory.GetContext();

var organization = await dbContext.Organizations
.AsNoTracking()
.Include(x => x.RemotelyUsers)
.FirstOrDefaultAsync(x => x.ID == orgId);

Expand All @@ -1198,7 +1212,9 @@ public async Task<Result<ApiToken>> GetApiKey(string keyId)

using var dbContext = _appDbFactory.GetContext();

var token = await dbContext.ApiTokens.FirstOrDefaultAsync(x => x.ID == keyId);
var token = await dbContext.ApiTokens
.AsNoTracking()
.FirstOrDefaultAsync(x => x.ID == keyId);

if (token is null)
{
Expand All @@ -1218,6 +1234,7 @@ public async Task<Result<BrandingInfo>> GetBrandingInfo(string organizationId)
using var dbContext = _appDbFactory.GetContext();

var organization = await dbContext.Organizations
.AsNoTracking()
.Include(x => x.BrandingInfo)
.FirstOrDefaultAsync(x => x.ID == organizationId);

Expand Down Expand Up @@ -1245,7 +1262,9 @@ public async Task<Result<Organization>> GetDefaultOrganization()
{
using var dbContext = _appDbFactory.GetContext();

var org = await dbContext.Organizations.FirstOrDefaultAsync(x => x.IsDefaultOrganization);
var org = await dbContext.Organizations
.AsNoTracking()
.FirstOrDefaultAsync(x => x.IsDefaultOrganization);

if (org is null)
{
Expand All @@ -1259,9 +1278,11 @@ public async Task<Result<Device>> GetDevice(string orgId, string deviceId)
{
using var dbContext = _appDbFactory.GetContext();

var device = await dbContext.Devices.FirstOrDefaultAsync(x =>
x.OrganizationID == orgId &&
x.ID == deviceId);
var device = await dbContext.Devices
.AsNoTracking()
.FirstOrDefaultAsync(x =>
x.OrganizationID == orgId &&
x.ID == deviceId);

if (device is null)
{
Expand All @@ -1276,8 +1297,12 @@ public async Task<Result<Device>> GetDevice(
{
using var dbContext = _appDbFactory.GetContext();

var query = dbContext.Devices.AsQueryable();
var query = dbContext.Devices
.AsNoTracking()
.AsQueryable();

includesBuilder?.Invoke(query);

var device = await query.FirstOrDefaultAsync(x => x.ID == deviceId);

if (device is null)
Expand All @@ -1304,6 +1329,7 @@ public int GetDeviceCount(RemotelyUser user)
}

return dbContext.Users
.AsNoTracking()
.Include(x => x.DeviceGroups)
.ThenInclude(x => x.Devices)
.Where(x => x.Id == user.Id)
Expand All @@ -1319,7 +1345,9 @@ public async Task<Result<DeviceGroup>> GetDeviceGroup(
{
using var dbContext = _appDbFactory.GetContext();

var query = dbContext.DeviceGroups.AsQueryable();
var query = dbContext.DeviceGroups
.AsNoTracking()
.AsQueryable();

if (includeDevices)
{
Expand All @@ -1343,7 +1371,9 @@ public DeviceGroup[] GetDeviceGroups(string username)
{
using var dbContext = _appDbFactory.GetContext();

var user = dbContext.Users.FirstOrDefault(x => x.UserName == username);
var user = dbContext.Users
.AsNoTracking()
.FirstOrDefault(x => x.UserName == username);

if (user is null)
{
Expand All @@ -1352,6 +1382,7 @@ public DeviceGroup[] GetDeviceGroups(string username)
var userId = user.Id;

var groupIds = dbContext.DeviceGroups
.AsNoTracking()
.Include(x => x.Users)
.ThenInclude(x => x.DeviceGroups)
.Where(x =>
Expand All @@ -1367,6 +1398,7 @@ public DeviceGroup[] GetDeviceGroups(string username)
if (groupIds.Any())
{
return dbContext.DeviceGroups
.AsNoTracking()
.Where(x => groupIds.Contains(x.ID))
.OrderBy(x => x.Name)
.ToArray();
Expand All @@ -1380,6 +1412,7 @@ public DeviceGroup[] GetDeviceGroupsForOrganization(string organizationId)
using var dbContext = _appDbFactory.GetContext();

return dbContext.DeviceGroups
.AsNoTracking()
.Include(x => x.Users)
.ThenInclude(x => x.DeviceGroups)
.Where(x => x.OrganizationID == organizationId)
Expand All @@ -1392,6 +1425,7 @@ public List<Device> GetDevices(IEnumerable<string> deviceIds)
using var dbContext = _appDbFactory.GetContext();

return dbContext.Devices
.AsNoTracking()
.Where(x => deviceIds.Contains(x.ID))
.ToList();
}
Expand Down Expand Up @@ -1455,8 +1489,8 @@ public async Task<Result<Organization>> GetOrganizationByUserName(string userNam

using var dbContext = _appDbFactory.GetContext();

var user = await dbContext
.Users
var user = await dbContext.Users
.AsNoTracking()
.Include(x => x.Organization)
.FirstOrDefaultAsync(x => x.UserName!.ToLower() == userName.ToLower());

Expand Down Expand Up @@ -1486,7 +1520,9 @@ public async Task<Result<string>> GetOrganizationNameById(string organizationId)
{
using var dbContext = _appDbFactory.GetContext();

var org = await dbContext.Organizations.FirstOrDefaultAsync(x => x.ID == organizationId);
var org = await dbContext.Organizations
.AsNoTracking()
.FirstOrDefaultAsync(x => x.ID == organizationId);

if (org is null)
{
Expand All @@ -1506,8 +1542,9 @@ public async Task<Result<string>> GetOrganizationNameByUserName(string userName)
using var dbContext = _appDbFactory.GetContext();

var user = await dbContext.Users
.Include(x => x.Organization)
.FirstOrDefaultAsync(x => x.UserName == userName);
.AsNoTracking()
.Include(x => x.Organization)
.FirstOrDefaultAsync(x => x.UserName == userName);

if (user is null)
{
Expand All @@ -1523,6 +1560,7 @@ public async Task<IEnumerable<ScriptRun>> GetPendingScriptRuns(string deviceId)
using var dbContext = _appDbFactory.GetContext();

var device = await dbContext.Devices
.AsNoTracking()
.Include(x => x.ScriptRuns)
.ThenInclude(x => x.Results)
.FirstOrDefaultAsync(x => x.ID == deviceId);
Expand Down Expand Up @@ -1559,6 +1597,7 @@ public async Task<Result<SavedScript>> GetSavedScript(string userId, Guid script
using var dbContext = _appDbFactory.GetContext();

var script = await dbContext.SavedScripts
.AsNoTracking()
.Include(x => x.Creator)
.FirstOrDefaultAsync(x =>
x.Id == scriptId &&
Expand All @@ -1574,7 +1613,9 @@ public async Task<Result<SavedScript>> GetSavedScript(string userId, Guid script
public async Task<Result<SavedScript>> GetSavedScript(Guid scriptId)
{
using var dbContext = _appDbFactory.GetContext();
var script = await dbContext.SavedScripts.FirstOrDefaultAsync(x => x.Id == scriptId);
var script = await dbContext.SavedScripts
.AsNoTracking()
.FirstOrDefaultAsync(x => x.Id == scriptId);

if (script is null)
{
Expand All @@ -1588,6 +1629,7 @@ public async Task<List<SavedScript>> GetSavedScriptsWithoutContent(string userId
using var dbContext = _appDbFactory.GetContext();

return await dbContext.SavedScripts
.AsNoTracking()
.Include(x => x.Creator)
.Where(x =>
x.Creator!.OrganizationID == organizationId &&
Expand All @@ -1611,6 +1653,7 @@ public async Task<Result<ScriptResult>> GetScriptResult(string resultId, string
using var dbContext = _appDbFactory.GetContext();

var result = await dbContext.ScriptResults
.AsNoTracking()
.FirstOrDefaultAsync(x =>
x.OrganizationID == orgId &&
x.ID == resultId);
Expand Down Expand Up @@ -1639,6 +1682,7 @@ public async Task<List<ScriptSchedule>> GetScriptSchedules(string organizationId
{
using var dbContext = _appDbFactory.GetContext();
return await dbContext.ScriptSchedules
.AsNoTracking()
.Include(x => x.Creator)
.Include(x => x.Devices)
.Include(x => x.DeviceGroups)
Expand All @@ -1653,6 +1697,7 @@ public async Task<List<ScriptSchedule>> GetScriptSchedulesDue()
var now = Time.Now;

return await dbContext.ScriptSchedules
.AsNoTracking()
.Include(x => x.Devices)
.Include(x => x.DeviceGroups)
.ThenInclude(x => x.Devices)
Expand All @@ -1665,6 +1710,7 @@ public List<string> GetServerAdmins()
using var dbContext = _appDbFactory.GetContext();

return dbContext.Users
.AsNoTracking()
.Where(x => x.IsServerAdmin)
.Select(x => $"{x.UserName}")
.ToList();
Expand Down Expand Up @@ -1698,7 +1744,9 @@ public async Task<Result<RemotelyUser>> GetUserById(string userId)
}
using var dbContext = _appDbFactory.GetContext();

var user = await dbContext.Users.FirstOrDefaultAsync(x => x.Id == userId);
var user = await dbContext.Users
.AsNoTracking()
.FirstOrDefaultAsync(x => x.Id == userId);

if (user is null)
{
Expand All @@ -1718,7 +1766,10 @@ public async Task<Result<RemotelyUser>> GetUserByName(

using var dbContext = _appDbFactory.GetContext();

var query = dbContext.Users.AsQueryable();
var query = dbContext.Users
.AsNoTracking()
.AsQueryable();

includesBuilder?.Invoke(query);

var user = await query.FirstOrDefaultAsync(x =>
Expand All @@ -1736,7 +1787,8 @@ public async Task<Result<RemotelyUserOptions>> GetUserOptions(string userName)
using var dbContext = _appDbFactory.GetContext();

var user = await dbContext.Users
.FirstOrDefaultAsync(x => x.UserName == userName);
.AsNoTracking()
.FirstOrDefaultAsync(x => x.UserName == userName);

if (user is null)
{
Expand All @@ -1758,24 +1810,27 @@ public async Task<Result> JoinViaInvitation(string userName, string inviteId)

using var dbContext = _appDbFactory.GetContext();

var invite = await dbContext.InviteLinks.FirstOrDefaultAsync(x =>
x.InvitedUser!.ToLower() == userName.ToLower() &&
x.ID == inviteId);
var invite = await dbContext.InviteLinks
.FirstOrDefaultAsync(x =>
x.InvitedUser!.ToLower() == userName.ToLower() &&
x.ID == inviteId);

if (invite is null)
{
return Result.Fail("Invite not found.");
}

var user = await dbContext.Users.FirstOrDefaultAsync(x => x.UserName == userName);
var user = await dbContext.Users
.FirstOrDefaultAsync(x => x.UserName == userName);

if (user is null)
{
return Result.Fail("User not found.");
}

var organization = await dbContext.Organizations
.Include(x => x.RemotelyUsers)
.FirstOrDefaultAsync(x => x.ID == invite.OrganizationID);
.Include(x => x.RemotelyUsers)
.FirstOrDefaultAsync(x => x.ID == invite.OrganizationID);

if (organization is null)
{
Expand Down

0 comments on commit 7a8809c

Please sign in to comment.