IW4M-Admin/Data/Context/ContextSeed.cs

50 lines
1.5 KiB
C#
Raw Normal View History

using System;
using System.Threading;
2018-04-08 02:44:42 -04:00
using System.Threading.Tasks;
using Data.Abstractions;
using Data.Models;
using Data.Models.Client;
using Microsoft.EntityFrameworkCore;
2018-04-08 02:44:42 -04:00
namespace Data.Context
2018-04-08 02:44:42 -04:00
{
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
{
await using 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
2022-01-28 10:35:01 -05:00
context.Clients.Add(new EFClient
2018-04-08 02:44:42 -04:00
{
Active = false,
Connections = 0,
FirstConnection = DateTime.UtcNow,
LastConnection = DateTime.UtcNow,
Level = EFClient.Permission.Console,
2018-04-08 02:44:42 -04:00
Masked = true,
NetworkId = 0,
AliasLink = link,
2022-01-28 10:35:01 -05:00
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
}
}
}
2022-01-28 10:35:01 -05:00
}