如果数据库不存在,自动创建数据库

This commit is contained in:
falcon 2020-12-03 15:12:34 +08:00
parent dca499910f
commit 9bd424b540
2 changed files with 1 additions and 33 deletions

View File

@ -1,32 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
namespace StaffManagement.Database
{
static public class DbInit
{
/// <summary>
/// 初始化数据库
/// </summary>
/// <param name="app"></param>
/// <returns></returns>
public static IApplicationBuilder SMDbInit(this IApplicationBuilder app) {
app.ApplicationServices.GetService<SMDbContext>().Init();
return app;
}
/// <summary>
/// 初始化数据库
/// </summary>
/// <param name="db">数据库上下文</param>
/// <returns></returns>
public static SMDbContext Init(this SMDbContext db) {
db.Database.EnsureCreated();
return db;
}
}
}

View File

@ -66,7 +66,7 @@ namespace StaffManagement
app.UseExceptionHandler("/Home/Error"); app.UseExceptionHandler("/Home/Error");
} }
app.UseStaticFiles(); app.UseStaticFiles();
db.Init(); db.Database.EnsureCreated();
app.UseSwagger(); app.UseSwagger();
app.UseSwaggerUI(c => { app.UseSwaggerUI(c => {
c.SwaggerEndpoint("/swagger/V1/swagger.json","接口文档"); c.SwaggerEndpoint("/swagger/V1/swagger.json","接口文档");