-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSeedData.cs
More file actions
64 lines (59 loc) · 2.32 KB
/
Copy pathSeedData.cs
File metadata and controls
64 lines (59 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Sample.Repository.Model;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Sample.Repository
{
public static class SeedData
{
public static async Task InitializeAsync(IServiceProvider serviceProvider)
{
IDbContextFactory<ChecklistContext> contextFactory = serviceProvider.GetRequiredService<IDbContextFactory<ChecklistContext>>();
using var context = await contextFactory.CreateDbContextAsync();
await InitializeAsync(context);
}
public static async Task InitializeAsync(ChecklistContext context)
{
if (context == null || context.Checklists == null)
{
throw new ArgumentNullException("Null Checklists");
}
// Look for any checklist.
if (await context.Checklists.AnyAsync())
{
return; // DB has been seeded
}
await context.Checklists.AddRangeAsync(
new Checklist
{
Name = "Checklist 1",
Date = DateTime.UtcNow,
Description = "Checklist 1 Description",
CheckItems = new List<CheckItem>
{
new CheckItem { Description = "CheckItem 1"},
new CheckItem { Description = "CheckItem 3"},
new CheckItem { Description = "CheckItem 4"},
new CheckItem { Description = "CheckItem 5"},
}
},
new Checklist
{
Name = "Checklist 2",
Date = DateTime.UtcNow,
Description = "Checklist 2 Description",
CheckItems = new List<CheckItem>
{
new CheckItem { Description = "CheckItem 1"},
new CheckItem { Description = "CheckItem 3"},
new CheckItem { Description = "CheckItem 4"},
new CheckItem { Description = "CheckItem 5"},
}
}
);
await context.SaveChangesAsync();
}
}
}