diff --git a/Data/BotDatabaseContext.cs b/Data/BotDatabaseContext.cs index 319594d..41482de 100644 --- a/Data/BotDatabaseContext.cs +++ b/Data/BotDatabaseContext.cs @@ -54,6 +54,10 @@ public class BotDatabaseContext : DbContext { modelBuilder.Entity(e => { e.HasKey(p => new { p.GuildId, p.UserId }); e.Property(p => p.FirstSeenTime).HasDefaultValueSql("now()"); + e.HasOne(entry => entry.User) + .WithMany(u => u.Guilds) + .HasForeignKey(entry => entry.UserId); + e.Navigation(entry => entry.User).AutoInclude(); }); modelBuilder.Entity(e => e.Property(p => p.CreatedAt).HasDefaultValueSql("now()")); modelBuilder.HasPostgresEnum(); @@ -62,6 +66,14 @@ public class BotDatabaseContext : DbContext { e.HasOne(entry => entry.User) .WithMany(gu => gu.Logs) .HasForeignKey(entry => new { entry.GuildId, entry.UserId }); + e.Navigation(entry => entry.User).AutoInclude(); + }); + modelBuilder.Entity(e => { + e.HasOne(entry => entry.Author) + .WithMany(gu => gu.Messages) + .HasForeignKey(cgm => new { cgm.GuildId, cgm.AuthorId }) + .HasPrincipalKey(cgu => new { cgu.GuildId, cgu.UserId }); + e.Navigation(entry => entry.Author).AutoInclude(); }); } } diff --git a/Data/CachedGuildMessage.cs b/Data/CachedGuildMessage.cs index 9734d5d..283c7c2 100644 --- a/Data/CachedGuildMessage.cs +++ b/Data/CachedGuildMessage.cs @@ -53,11 +53,8 @@ public class CachedGuildMessage { /// public string? Content { get; set; } = null!; - /// - [ForeignKey(nameof(AuthorId))] - [InverseProperty(nameof(CachedUser.GuildMessages))] - public CachedUser Author { get; set; } = null!; - // TODO set up composite foreign key. will require rewriting some parts in modules... + /// + public CachedGuildUser Author { get; set; } = null!; // Used by MessageCachingSubservice internal static CachedGuildMessage? Clone(CachedGuildMessage? original) { diff --git a/Data/CachedGuildUser.cs b/Data/CachedGuildUser.cs index 1995702..6b50001 100644 --- a/Data/CachedGuildUser.cs +++ b/Data/CachedGuildUser.cs @@ -28,14 +28,17 @@ public class CachedGuildUser { public string? Nickname { get; set; } /// - /// If included in the query, references the associated for this entry. + /// Gets the associated for this entry. This entity is auto-included. /// - [ForeignKey(nameof(UserId))] - [InverseProperty(nameof(CachedUser.Guilds))] public CachedUser User { get; set; } = null!; /// /// If included in the query, references all items associated with this entry. /// public ICollection Logs { get; set; } = null!; + + /// + /// If included in the query, references all items associated with this entry. + /// + public ICollection Messages { get; set; } = null!; } diff --git a/Data/CachedUser.cs b/Data/CachedUser.cs index 04d4d01..be8c3fa 100644 --- a/Data/CachedUser.cs +++ b/Data/CachedUser.cs @@ -37,12 +37,10 @@ public class CachedUser { /// /// If included in the query, gets the list of associated entries for this entry. /// - [InverseProperty(nameof(CachedGuildUser.User))] public ICollection Guilds { get; set; } = null!; /// /// If included in the query, gets the list of associated entries for this entry. /// - [InverseProperty(nameof(CachedGuildMessage.Author))] public ICollection GuildMessages { get; set; } = null!; } diff --git a/Data/ModLogEntry.cs b/Data/ModLogEntry.cs index 1f6e06e..7bc6a7f 100644 --- a/Data/ModLogEntry.cs +++ b/Data/ModLogEntry.cs @@ -44,7 +44,7 @@ public class ModLogEntry : ISharedEvent { public string? Message { get; set; } /// - /// If included in the query, gets the associated for this entry. + /// Gets the associated for this entry. This entity is auto-included. /// public CachedGuildUser User { get; set; } = null!; } \ No newline at end of file diff --git a/Modules/ModLogs/ModLogs_Messages.cs b/Modules/ModLogs/ModLogs_Messages.cs index 625a85f..f826ab4 100644 --- a/Modules/ModLogs/ModLogs_Messages.cs +++ b/Modules/ModLogs/ModLogs_Messages.cs @@ -1,5 +1,4 @@ using Discord; -using Microsoft.EntityFrameworkCore; using RegexBot.Data; using System.Text; @@ -24,7 +23,6 @@ internal partial class ModLogs { using var db = new BotDatabaseContext(); var cachedMsg = db.GuildMessageCache - .Include(gm => gm.Author) .Where(gm => gm.MessageId == argMsg.Id) .SingleOrDefault(); @@ -50,8 +48,8 @@ internal partial class ModLogs { }; } else { reportEmbed.Author = new EmbedAuthorBuilder() { - Name = $"{cachedMsg.Author.Username}#{cachedMsg.Author.Discriminator}", - IconUrl = cachedMsg.Author.AvatarUrl ?? GetDefaultAvatarUrl(cachedMsg.Author.Discriminator) + Name = $"{cachedMsg.Author.User.Username}#{cachedMsg.Author.User.Discriminator}", + IconUrl = cachedMsg.Author.User.AvatarUrl ?? GetDefaultAvatarUrl(cachedMsg.Author.User.Discriminator) }; } SetAttachmentsField(reportEmbed, cachedMsg.AttachmentNames); diff --git a/Services/EntityCache/UserCachingSubservice.cs b/Services/EntityCache/UserCachingSubservice.cs index bc369b0..f07010f 100644 --- a/Services/EntityCache/UserCachingSubservice.cs +++ b/Services/EntityCache/UserCachingSubservice.cs @@ -1,5 +1,4 @@ #pragma warning disable CA1822 // "Mark members as static" - members should only be callable by code with access to this instance -using Microsoft.EntityFrameworkCore; using RegexBot.Common; using RegexBot.Data; @@ -112,7 +111,7 @@ class UserCachingSubservice { internal CachedGuildUser? DoGuildUserQuery(ulong guildId, string search) { static CachedGuildUser? innerQuery(ulong guildId, ulong? sID, (string name, string? disc)? nameSearch) { var db = new BotDatabaseContext(); - var query = db.GuildUserCache.Include(gu => gu.User).Where(c => c.GuildId == guildId); + var query = db.GuildUserCache.Where(c => c.GuildId == guildId); if (sID.HasValue) query = query.Where(c => c.UserId == sID.Value); if (nameSearch != null) {