add migration and env to Api

This commit is contained in:
YuukanOO 2020-12-29 21:17:42 +01:00
parent 0972ec304a
commit 0d8082448d
2 changed files with 16 additions and 0 deletions

View File

@ -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
});
});
}
/// <summary>
/// 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.
/// </summary>
/// <param name="app"></param>
private void MigrateDatabase(IApplicationBuilder app)
{
using var scope = app.ApplicationServices.CreateScope();
using var ctx = scope.ServiceProvider.GetRequiredService<HNDbContext>();
ctx.Database.Migrate();
}
}
}

View File

@ -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"]