Foxchat.NET/Foxchat.Chat/Database/ChatContext.cs

53 lines
1.6 KiB
C#
Raw Normal View History

2024-05-14 22:24:53 +02:00
using Foxchat.Core;
using Foxchat.Core.Database;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
namespace Foxchat.Chat.Database;
public class ChatContext : IDatabaseContext
{
private readonly string _connString;
public override DbSet<Instance> Instance { get; set; }
public ChatContext(InstanceConfig config)
{
_connString = new Npgsql.NpgsqlConnectionStringBuilder(config.Database.Url)
{
Timeout = config.Database.Timeout ?? 5,
MaxPoolSize = config.Database.MaxPoolSize ?? 50,
}.ConnectionString;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseNpgsql(_connString)
.UseSnakeCaseNamingConvention();
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
// ULIDs are stored as UUIDs in the database
configurationBuilder.Properties<Ulid>().HaveConversion<UlidConverter>();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
}
}
public class DesignTimeIdentityContextFactory : IDesignTimeDbContextFactory<ChatContext>
{
public ChatContext CreateDbContext(string[] args)
{
// Read the configuration file
var config = new ConfigurationBuilder()
.AddConfiguration("identity.ini")
.Build()
// Get the configuration as our config class
.Get<InstanceConfig>() ?? new();
return new ChatContext(config);
}
}