Don't reseed on non empty database
This commit is contained in:
@@ -22,24 +22,16 @@ public static class DatabaseSeeder
|
|||||||
{
|
{
|
||||||
await context.Database.MigrateAsync();
|
await context.Database.MigrateAsync();
|
||||||
|
|
||||||
// Seed workplaces
|
// Seed workplaces only if none exist yet
|
||||||
var existing = await context.Workplaces.ToListAsync();
|
if (!await context.Workplaces.AnyAsync())
|
||||||
var expectedSet = ExpectedWorkplaces.Select(w => w.Name + w.Location).ToHashSet();
|
|
||||||
var existingSet = existing.Select(w => w.Name + w.Location).ToHashSet();
|
|
||||||
|
|
||||||
if (!expectedSet.SetEquals(existingSet))
|
|
||||||
{
|
{
|
||||||
context.Workplaces.RemoveRange(existing);
|
|
||||||
await context.SaveChangesAsync();
|
|
||||||
|
|
||||||
var workplaces = ExpectedWorkplaces.Select(w => new Workplace(w.Name, w.Location));
|
var workplaces = ExpectedWorkplaces.Select(w => new Workplace(w.Name, w.Location));
|
||||||
context.Workplaces.AddRange(workplaces);
|
context.Workplaces.AddRange(workplaces);
|
||||||
await context.SaveChangesAsync();
|
await context.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Seed admin user
|
// Seed admin user only if no users exist yet
|
||||||
var adminExists = await context.Users.AnyAsync(u => u.Email == AdminEmail);
|
if (!await context.Users.AnyAsync())
|
||||||
if (!adminExists)
|
|
||||||
{
|
{
|
||||||
var hash = passwordHasher.Hash(AdminPassword);
|
var hash = passwordHasher.Hash(AdminPassword);
|
||||||
var admin = User.Create(AdminEmail, "Admin", hash, isAdmin: true).Value!;
|
var admin = User.Create(AdminEmail, "Admin", hash, isAdmin: true).Value!;
|
||||||
|
|||||||
Reference in New Issue
Block a user