IW4M-Admin/SharedLibraryCore/Database/ContextSeed.cs

50 lines
1.6 KiB
C#
Raw Normal View History

2018-04-08 02:44:42 -04:00
using Microsoft.EntityFrameworkCore;
using SharedLibraryCore.Database.Models;
using System;
using System.Linq;
using System.Threading;
2018-04-08 02:44:42 -04:00
using System.Threading.Tasks;
using SharedLibraryCore.Interfaces;
using static SharedLibraryCore.Database.Models.EFClient;
2018-04-08 02:44:42 -04:00
namespace SharedLibraryCore.Database
{
public static class ContextSeed
2018-04-08 02:44:42 -04:00
{
public static async Task Seed(IDatabaseContextFactory contextFactory, CancellationToken token)
2018-04-08 02:44:42 -04:00
{
var context = contextFactory.CreateContext();
var strategy = context.Database.CreateExecutionStrategy();
await strategy.ExecuteAsync(async () =>
{
await context.Database.MigrateAsync(token);
});
if (!await context.AliasLinks.AnyAsync(token))
2018-04-08 02:44:42 -04:00
{
var link = new EFAliasLink();
2018-04-08 02:44:42 -04:00
context.Clients.Add(new EFClient()
{
Active = false,
Connections = 0,
FirstConnection = DateTime.UtcNow,
LastConnection = DateTime.UtcNow,
Level = Permission.Console,
2018-04-08 02:44:42 -04:00
Masked = true,
NetworkId = 0,
AliasLink = link,
CurrentAlias = new EFAlias()
{
Link = link,
Active = true,
DateAdded = DateTime.UtcNow,
Name = "IW4MAdmin",
},
2018-04-08 02:44:42 -04:00
});
await context.SaveChangesAsync(token);
2018-04-08 02:44:42 -04:00
}
}
}
}