datenbank immer automatisch updaten

This commit is contained in:
hydrant
2020-04-24 21:56:27 +02:00
parent 1557618aeb
commit 398a010776

View File

@@ -22,7 +22,7 @@ using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Infrastructure;
using ReallifeGamemode.Database.Entities; using ReallifeGamemode.Database.Entities;
using ReallifeGamemode.Services; using ReallifeGamemode.Services;
using System.Threading;
/** /**
* @overview Life of German Reallife - Main Class (Main.cs) * @overview Life of German Reallife - Main Class (Main.cs)
@@ -44,6 +44,36 @@ namespace ReallifeGamemode.Server
[ServerEvent(Event.ResourceStart)] [ServerEvent(Event.ResourceStart)]
public void OnResourceStart() public void OnResourceStart()
{ {
using var dbContext = new DatabaseContext(true);
var pendingMigrations = dbContext.Database.GetPendingMigrations();
if (!pendingMigrations.Any())
{
System.Console.WriteLine("No migrations to apply");
}
else
{
System.Console.WriteLine("Applying {0} migrations", pendingMigrations.Count());
foreach (var migration in pendingMigrations)
{
System.Console.WriteLine("\t{0}", migration);
}
try
{
dbContext.Database.Migrate();
}
catch (System.Exception e)
{
System.Console.WriteLine("Error while updating database: {0}", e.ToString());
System.Console.ReadLine();
System.Environment.Exit(1);
}
System.Console.WriteLine("Migrations successfull");
}
var methods = Assembly.GetExecutingAssembly() var methods = Assembly.GetExecutingAssembly()
.GetTypes() .GetTypes()
.SelectMany(t => t.GetMethods()) .SelectMany(t => t.GetMethods())