From 0d8082448d5618779c4487e82103fe74095d879f Mon Sep 17 00:00:00 2001 From: YuukanOO Date: Tue, 29 Dec 2020 21:17:42 +0100 Subject: [PATCH] add migration and env to Api --- Apps/Api/Startup.cs | 15 +++++++++++++++ Dockerfile | 1 + 2 files changed, 16 insertions(+) diff --git a/Apps/Api/Startup.cs b/Apps/Api/Startup.cs index 7bc2a8c..ef15b70 100644 --- a/Apps/Api/Startup.cs +++ b/Apps/Api/Startup.cs @@ -9,6 +9,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc.Authorization; using Microsoft.AspNetCore.Routing; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -106,6 +107,8 @@ namespace Api // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { + MigrateDatabase(app); + app.UseOpenApi(); if (env.IsDevelopment()) @@ -135,5 +138,17 @@ namespace Api }); }); } + + /// + /// Lance les migrations. En production, il est plutôt conseillé de générer + /// les scripts avec `dotnet ef migrations script` et de les passer à la main. + /// + /// + private void MigrateDatabase(IApplicationBuilder app) + { + using var scope = app.ApplicationServices.CreateScope(); + using var ctx = scope.ServiceProvider.GetRequiredService(); + ctx.Database.Migrate(); + } } } diff --git a/Dockerfile b/Dockerfile index 1cdc262..9e75f60 100644 --- a/Dockerfile +++ b/Dockerfile @@ -22,5 +22,6 @@ RUN dotnet publish -c release -o /app --no-restore FROM mcr.microsoft.com/dotnet/aspnet:5.0 WORKDIR /app COPY --from=build /app ./ +ENV "ConnectionStrings:Default"="Data Source=hn.db" EXPOSE 80 ENTRYPOINT ["dotnet", "Api.dll"] \ No newline at end of file