Add a DB init service when there is not database.

This commit is contained in:
Sébastien André
2020-12-18 18:28:30 +01:00
parent f2875eb969
commit 2991a132bc
15 changed files with 1267 additions and 145 deletions

View File

@@ -1,5 +1,8 @@
using System.Linq;
using System.Text;
using System.IO;
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
@@ -11,7 +14,7 @@ using Microsoft.IdentityModel.Tokens;
using AutoMapper;
using skydiveLogs_api.Ioc;
using skydiveLogs_api.Business.Interface;
namespace skydiveLogs_api
{
@@ -69,6 +72,8 @@ namespace skydiveLogs_api
// AutoMapper
services.AddAutoMapper(typeof(Mapper.ModelProfile));
CheckAndInitDb(services);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
@@ -90,6 +95,20 @@ namespace skydiveLogs_api
app.UseMvc();
}
private void CheckAndInitDb(IServiceCollection services)
{
string connectionString = Configuration.GetConnectionString("DefaultConnection");
string dbFile = connectionString.Replace("Filename=", string.Empty);
if (!File.Exists(dbFile))
{
var serviceProvider = services.BuildServiceProvider();
var initDbService = serviceProvider.GetRequiredService<IInitDbService>();
initDbService.GenerateDb();
}
}
public IConfiguration Configuration { get; }
}
}