sync full source from private myoffice
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
namespace MyOffice.DbContext;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
public static class DbContextServiceCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection AddAppDbContext(
|
||||
this IServiceCollection services,
|
||||
ConnectionConfiguration connection
|
||||
)
|
||||
{
|
||||
services.AddSingleton(connection);
|
||||
|
||||
services.AddDbContext<AppDbContext>((_, options) =>
|
||||
{
|
||||
ConfigureDbContextOptions(options, connection);
|
||||
});
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static void ConfigureDbContextOptions(
|
||||
DbContextOptionsBuilder options,
|
||||
ConnectionConfiguration connection
|
||||
)
|
||||
{
|
||||
if (!Enum.TryParse<AppDbContextProvidersEnum>(connection.Provider, ignoreCase: true, out var provider))
|
||||
throw new InvalidOperationException($"No such provider: [{connection.Provider}]");
|
||||
|
||||
switch (provider)
|
||||
{
|
||||
case AppDbContextProvidersEnum.npgsql:
|
||||
options.UseNpgsql(connection.ConnectionString, x =>
|
||||
x.MigrationsAssembly("MyOffice.Migrations.Postgres"));
|
||||
break;
|
||||
case AppDbContextProvidersEnum.sqlite:
|
||||
options.UseSqlite(connection.ConnectionString, x =>
|
||||
x.MigrationsAssembly("MyOffice.Migrations.Sqlite"));
|
||||
break;
|
||||
default:
|
||||
throw new NotSupportedException($"No such provider: [{connection.Provider}]");
|
||||
}
|
||||
|
||||
#if DEBUG
|
||||
options.EnableSensitiveDataLogging();
|
||||
options.EnableDetailedErrors();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user