Add project files.
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
namespace MyOffice.DbContext;
|
||||
|
||||
using Data.Models.Account;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Data.Models.Users;
|
||||
|
||||
public enum AppDbContextProvidersEnum
|
||||
{
|
||||
sqlite,
|
||||
mssql,
|
||||
npgsql
|
||||
}
|
||||
|
||||
public class AppDbContext : DbContext
|
||||
{
|
||||
private readonly AppDbContextProvidersEnum _provider;
|
||||
|
||||
private readonly Dictionary<AppDbContextProvidersEnum, string> _noCaseCollation = new()
|
||||
{
|
||||
{ AppDbContextProvidersEnum.sqlite, "NOCASE" },
|
||||
{ AppDbContextProvidersEnum.npgsql, "my_ci_collation" }
|
||||
};
|
||||
|
||||
public AppDbContext(AppDbContextProvidersEnum provider, DbContextOptions<AppDbContext> options) : base(options)
|
||||
{
|
||||
_provider = provider;
|
||||
}
|
||||
|
||||
public DbSet<User> Users { get; set; } = null!;
|
||||
public DbSet<UserExternal> UserClaims { get; set; } = null!;
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
var noCaseCollation = _noCaseCollation[_provider];
|
||||
|
||||
if (_provider == AppDbContextProvidersEnum.npgsql)
|
||||
modelBuilder.HasCollation("my_ci_collation", "en-u-ks-primary", "icu", false);
|
||||
|
||||
/* NOCASE PROPERTIES */
|
||||
modelBuilder.Entity<User>()
|
||||
.Property(x => x.UserName)
|
||||
.UseCollation(noCaseCollation);
|
||||
|
||||
modelBuilder.Entity<User>()
|
||||
.Property(x => x.Email)
|
||||
.UseCollation(noCaseCollation);
|
||||
|
||||
modelBuilder.Entity<UserExternal>()
|
||||
.Property(x => x.Provider)
|
||||
.UseCollation(noCaseCollation);
|
||||
|
||||
modelBuilder.Entity<UserExternal>()
|
||||
.Property(x => x.Email)
|
||||
.UseCollation(noCaseCollation);
|
||||
|
||||
|
||||
modelBuilder.Entity<UserExternal>()
|
||||
.HasOne(x => x.User)
|
||||
.WithMany(x => x.UserClaims)
|
||||
.HasForeignKey(x => x.UserId);
|
||||
|
||||
CurrencyCreating(modelBuilder);
|
||||
AccountCreating(modelBuilder);
|
||||
|
||||
base.OnModelCreating(modelBuilder);
|
||||
}
|
||||
|
||||
private void CurrencyCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.Entity<CurrencyGlobal>()
|
||||
.HasKey(x => x.Id);
|
||||
|
||||
modelBuilder.Entity<Currency>()
|
||||
.HasKey(x => x.Id);
|
||||
|
||||
modelBuilder.Entity<CurrencyRate>()
|
||||
.HasKey(x => x.Id);
|
||||
|
||||
modelBuilder.Entity<Currency>()
|
||||
.HasOne(x => x.CurrencyGlobal)
|
||||
.WithMany(x => x.Currencies)
|
||||
.HasForeignKey(x => x.CurrencyGlobalId);
|
||||
|
||||
modelBuilder.Entity<Currency>()
|
||||
.HasOne(x => x.User)
|
||||
.WithMany(x => x.Currencies)
|
||||
.HasForeignKey(x => x.UserId);
|
||||
|
||||
modelBuilder.Entity<CurrencyRate>()
|
||||
.HasOne(x => x.Currency)
|
||||
.WithMany(x => x.Rates)
|
||||
.HasForeignKey(x => x.CurrencyId);
|
||||
}
|
||||
|
||||
private void AccountCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.Entity<Account>()
|
||||
.HasKey(x => x.Id);
|
||||
|
||||
modelBuilder.Entity<AccountAccess>()
|
||||
.HasKey(x => x.Id);
|
||||
|
||||
modelBuilder.Entity<AccountMotion>()
|
||||
.HasKey(x => x.Id);
|
||||
|
||||
modelBuilder.Entity<Account>()
|
||||
.HasOne(x => x.CurrencyGlobal)
|
||||
.WithMany(x => x.Accounts)
|
||||
.HasForeignKey(x => x.CurrencyGlobalId);
|
||||
|
||||
modelBuilder.Entity<AccountAccess>()
|
||||
.HasOne(x => x.Account)
|
||||
.WithMany(x => x.AccessRights)
|
||||
.HasForeignKey(x => x.AccountId);
|
||||
|
||||
modelBuilder.Entity<AccountAccess>()
|
||||
.HasOne(x => x.User)
|
||||
.WithMany(x => x.AccessRights)
|
||||
.HasForeignKey(x => x.AccountId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user