using Microsoft.EntityFrameworkCore; namespace RegexBot.Data; /// /// Represents a database connection using the settings defined in the bot's global configuration. /// public class BotDatabaseContext : DbContext { private static string? _npgsqlConnectionString; internal static string PostgresConnectionString { #if DEBUG get { if (_npgsqlConnectionString != null) return _npgsqlConnectionString; Console.WriteLine($"{nameof(RegexBot)} - {nameof(BotDatabaseContext)} note: Using hardcoded connection string!"); return _npgsqlConnectionString ?? "Host=localhost;Username=regexbot;Password=rb"; } #else get => _npgsqlConnectionString!; #endif set => _npgsqlConnectionString ??= value; } /// /// Retrieves the user cache. /// public DbSet UserCache { get; set; } = null!; /// /// Retrieves the guild user cache. /// public DbSet GuildUserCache { get; set; } = null!; /// /// Retrieves the guild message cache. /// public DbSet GuildMessageCache { get; set; } = null!; /// protected sealed override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder .UseNpgsql(PostgresConnectionString) .UseSnakeCaseNamingConvention(); /// protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity(entity => entity.Property(e => e.Discriminator).HasMaxLength(4).IsFixedLength()); modelBuilder.Entity(entity => { entity.HasKey(e => new { e.UserId, e.GuildId }); entity.Property(e => e.FirstSeenTime).HasDefaultValueSql("now()"); }); modelBuilder.Entity(entity => entity.Property(e => e.CreatedAt).HasDefaultValueSql("now()")); } }