30 lines
921 B
C#
30 lines
921 B
C#
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Cloud.Middlewares;
|
|
|
|
public static class DatabaseMiddleware
|
|
{
|
|
public static void AddDbConnectionService(this IServiceCollection services)
|
|
{
|
|
string connectionString = Environment.GetEnvironmentVariable("DB_CONNECTION_STRING")
|
|
?? "Host=localhost;Port=5438;Database=main_database;Username=postgres;Password=12345";
|
|
|
|
services.AddDbContext<ApplicationContext>(options =>
|
|
options.UseNpgsql(connectionString));
|
|
|
|
}
|
|
public static void MigrateDb(this IApplicationBuilder app)
|
|
{
|
|
try
|
|
{
|
|
using var scope = app.ApplicationServices.CreateScope();
|
|
var context = scope.ServiceProvider.GetRequiredService<ApplicationContext>();
|
|
|
|
context.Database.Migrate();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
}
|
|
}
|
|
} |