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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
namespace MyOffice.DbContext;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Design;
|
||||
|
||||
public class AppDbContextFactory : IDesignTimeDbContextFactory<AppDbContext>
|
||||
{
|
||||
public static AppDbContextFactory Instance = new();
|
||||
|
||||
public AppDbContext CreateDbContext()
|
||||
{
|
||||
return CreateDbContext(null);
|
||||
}
|
||||
|
||||
public AppDbContext CreateDbContext(string[]? args)
|
||||
{
|
||||
var connection = RepositoryInitializer.ConnectionString;
|
||||
|
||||
var builder = new DbContextOptionsBuilder<AppDbContext>();
|
||||
|
||||
var provider = connection?.Provider ?? args?[0] ?? "";
|
||||
var connectionString = connection?.ConnectionString ?? args?[1] ?? "";
|
||||
|
||||
if (!Enum.TryParse<AppDbContextProvidersEnum>(provider, out var providerEnum))
|
||||
throw new InvalidOperationException($"No such provider: [{provider}]");
|
||||
|
||||
switch (providerEnum)
|
||||
{
|
||||
case AppDbContextProvidersEnum.npgsql:
|
||||
builder.UseNpgsql(connectionString, x => x.MigrationsAssembly("MyOffice.Migrations.Postgres"));
|
||||
break;
|
||||
case AppDbContextProvidersEnum.sqlite:
|
||||
builder.UseSqlite(connectionString, x => x.MigrationsAssembly("MyOffice.Migrations.Sqlite"));
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new NotSupportedException($"No such provider: [{provider}]");
|
||||
}
|
||||
|
||||
var db = new AppDbContext(providerEnum, builder.Options);
|
||||
db.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
|
||||
|
||||
return db;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.0" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MyOffice.Data.Models\MyOffice.Data.Models.csproj" />
|
||||
<ProjectReference Include="..\MyOffice.Shared\MyOffice.Shared.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,26 @@
|
||||
namespace MyOffice.DbContext;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
public record ConnectionConfiguration(string Provider, string ConnectionString);
|
||||
|
||||
public class RepositoryInitializer
|
||||
{
|
||||
public static void Initialize(
|
||||
IServiceCollection services,
|
||||
ConnectionConfiguration connectionString
|
||||
)
|
||||
{
|
||||
Services = services!;
|
||||
ConnectionString = connectionString;
|
||||
|
||||
var db = AppDbContextFactory.Instance.CreateDbContext();
|
||||
db.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
|
||||
//db.Database.EnsureCreated();
|
||||
db.Database.Migrate();
|
||||
}
|
||||
|
||||
public static ConnectionConfiguration ConnectionString { get; internal set; } = null!;
|
||||
public static IServiceCollection Services { get; internal set; } = null!;
|
||||
}
|
||||
Reference in New Issue
Block a user