don't track EFCore objects that don't need to be updated
This commit is contained in:
parent
d42e73699b
commit
f7f88ff98f
24 changed files with 50 additions and 33 deletions
|
|
@ -15,6 +15,7 @@
|
|||
|
||||
using Catalogger.Backend.Database.Models;
|
||||
using Catalogger.Backend.Extensions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Remora.Rest.Core;
|
||||
|
||||
namespace Catalogger.Backend.Database.Queries;
|
||||
|
|
@ -24,22 +25,30 @@ public static class QueryExtensions
|
|||
public static async ValueTask<Guild> GetGuildAsync(
|
||||
this DatabaseContext db,
|
||||
Snowflake id,
|
||||
bool shouldTrack = true,
|
||||
CancellationToken ct = default
|
||||
) => await db.GetGuildAsync(id.ToUlong(), ct);
|
||||
) => await db.GetGuildAsync(id.ToUlong(), shouldTrack, ct);
|
||||
|
||||
public static async ValueTask<Guild> GetGuildAsync(
|
||||
this DatabaseContext db,
|
||||
Optional<Snowflake> id,
|
||||
bool shouldTrack = true,
|
||||
CancellationToken ct = default
|
||||
) => await db.GetGuildAsync(id.ToUlong(), ct);
|
||||
) => await db.GetGuildAsync(id.ToUlong(), shouldTrack, ct);
|
||||
|
||||
public static async ValueTask<Guild> GetGuildAsync(
|
||||
this DatabaseContext db,
|
||||
ulong id,
|
||||
bool shouldTrack = true,
|
||||
CancellationToken ct = default
|
||||
)
|
||||
{
|
||||
var guild = await db.Guilds.FindAsync([id], ct);
|
||||
Guild? guild;
|
||||
if (shouldTrack)
|
||||
guild = await db.Guilds.FindAsync([id], ct);
|
||||
else
|
||||
guild = await db.Guilds.AsNoTracking().FirstOrDefaultAsync(g => g.Id == id, ct);
|
||||
|
||||
if (guild == null)
|
||||
throw new CataloggerError("Guild not found, was not initialized during guild create");
|
||||
return guild;
|
||||
|
|
@ -50,5 +59,8 @@ public static class QueryExtensions
|
|||
Snowflake guildId,
|
||||
Snowflake userId,
|
||||
CancellationToken ct = default
|
||||
) => await db.Watchlists.FindAsync([guildId.Value, userId.Value], ct);
|
||||
) =>
|
||||
await db
|
||||
.Watchlists.AsNoTracking()
|
||||
.FirstOrDefaultAsync(w => w.GuildId == guildId.Value && w.UserId == userId.Value, ct);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue