You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
978 B
31 lines
978 B
using Microsoft.EntityFrameworkCore; |
|
|
|
namespace BuaaLocationServer.Middlewares.Db |
|
{ |
|
public static class BuaaDbContextExtensions |
|
{ |
|
/// <summary> |
|
/// 运行迁移 |
|
/// </summary> |
|
/// <param name="app"></param> |
|
/// <returns></returns> |
|
public static IApplicationBuilder SetupDatabase(this WebApplication app) |
|
{ |
|
using var scope = app.Services.CreateScope(); |
|
var serviceProvidoer = scope.ServiceProvider; |
|
try |
|
{ |
|
var context = serviceProvidoer.GetRequiredService<BuaaDbContext>(); |
|
context.Database.Migrate(); |
|
context.SeedDefaultData(); |
|
} |
|
catch (Exception e) |
|
{ |
|
var logger = serviceProvidoer.GetRequiredService<ILogger<Program>>(); |
|
logger.LogError(e, "在设置数据库过程中发生了错误!"); |
|
} |
|
|
|
return app; |
|
} |
|
} |
|
}
|
|
|