Skip to content

Commit

Permalink
Refactor database seeding and CustomWebApplicationFactory (#740)
Browse files Browse the repository at this point in the history
  • Loading branch information
samanazadi1996 committed May 17, 2024
1 parent 8bfdf7d commit e0eaa4c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/Clean.Architecture.Infrastructure/Data/SeedData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ namespace Clean.Architecture.Infrastructure.Data;

public static class SeedData
{
public static readonly Contributor Contributor1 = new("Ardalis");
public static readonly Contributor Contributor2 = new("Snowfrog");

public static async Task InitializeAsync(AppDbContext dbContext)
{
if (await dbContext.Contributors.AnyAsync()) return; // DB has been seeded

await PopulateTestDataAsync(dbContext);
}

private static async Task PopulateTestDataAsync(AppDbContext dbContext)
public static async Task PopulateTestDataAsync(AppDbContext dbContext)
{
Contributor[] Contributors = [new("Ardalis"), new("Snowfrog")];

dbContext.Contributors.AddRange(Contributors);
dbContext.Contributors.AddRange([Contributor1, Contributor2]);
await dbContext.SaveChangesAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected override IHost CreateHost(IHostBuilder builder)
//if (!db.ToDoItems.Any())
//{
// Seed the database with test data.
SeedData.PopulateTestData(db);
SeedData.PopulateTestDataAsync(db).Wait();
//}
}
catch (Exception ex)
Expand Down

0 comments on commit e0eaa4c

Please sign in to comment.