48 lines
1.8 KiB
C#
48 lines
1.8 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using SewerStammGen.EntityFramework;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace SewerStammGen.HostBuilders
|
|
{
|
|
static class AddDBContextHostBuilderExtensions
|
|
{
|
|
public static IHostBuilder AddDBContext(this IHostBuilder hostBuilder)
|
|
{
|
|
hostBuilder.ConfigureServices((context, services) =>
|
|
{
|
|
string? connectionString = "";
|
|
Action<DbContextOptionsBuilder>? configureDbContext = null;
|
|
string? databaseToUse = context.Configuration.GetConnectionString("databaseToUse");
|
|
if (databaseToUse != null)
|
|
{
|
|
|
|
if (databaseToUse.Equals("default"))
|
|
{
|
|
connectionString = context.Configuration.GetConnectionString("default");
|
|
configureDbContext = o => o.UseNpgsql(connectionString);
|
|
}
|
|
else if (databaseToUse.Equals("sqlite"))
|
|
{
|
|
connectionString = context.Configuration.GetConnectionString("sqlite");
|
|
configureDbContext = o => o.UseSqlite(connectionString);
|
|
}
|
|
services.AddDbContext<SewerStammGenDbContext>(configureDbContext);
|
|
services.AddSingleton<SewerStammGenDbContextFactory>(new SewerStammGenDbContextFactory(configureDbContext));
|
|
}
|
|
|
|
|
|
|
|
});
|
|
return hostBuilder;
|
|
}
|
|
}
|
|
}
|