Add a DB init service when there is not database.
This commit is contained in:
@@ -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; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user