Skip to content

Commit 837b134

Browse files
DetysMarkPieszak
authored andcommitted
fix(seeding): seeding now follows .NET Core 2.0 best practices
1 parent 55f0105 commit 837b134

File tree

4 files changed

+80
-34
lines changed

4 files changed

+80
-34
lines changed

Program.cs

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,42 @@
66
using Microsoft.Extensions.Logging;
77
using System;
88
using System.IO;
9+
using System.Threading.Tasks;
910

10-
public class Program {
11-
public static void Main (string[] args) {
12-
var host = BuildWebHost (args);
13-
using (var scope = host.Services.CreateScope ()) {
14-
var services = scope.ServiceProvider;
15-
try {
16-
var context = services.GetRequiredService<SpaDbContext>();
17-
DbInitializer.Initialize(context);
18-
} catch (Exception ex) {
19-
var logger = services.GetRequiredService<ILogger<Program>> ();
20-
logger.LogError (ex, "An error occurred while seeding the database.");
21-
}
22-
}
11+
public class Program
12+
{
13+
public static async Task Main(string[] args)
14+
{
15+
var host = BuildWebHost(args);
16+
using (var scope = host.Services.CreateScope())
17+
{
18+
var services = scope.ServiceProvider;
2319

24-
host.Run ();
25-
}
26-
public static IWebHost BuildWebHost (string[] args) =>
27-
WebHost.CreateDefaultBuilder (args)
28-
.UseKestrel ()
29-
.UseContentRoot (Directory.GetCurrentDirectory ())
30-
.UseIISIntegration ()
31-
.UseStartup<Startup> ()
32-
.Build ();
33-
}
20+
try
21+
{
22+
await EnsureDataStorageIsReady(services);
23+
24+
} catch (Exception ex)
25+
{
26+
var logger = services.GetRequiredService<ILogger<Program>>();
27+
logger.LogError(ex, "An error occurred while seeding the database.");
28+
}
29+
}
30+
31+
host.Run();
32+
}
33+
public static IWebHost BuildWebHost(string[] args) =>
34+
WebHost.CreateDefaultBuilder(args)
35+
.UseKestrel()
36+
.UseContentRoot(Directory.GetCurrentDirectory())
37+
.UseIISIntegration()
38+
.UseStartup<Startup>()
39+
.Build();
40+
41+
private static async Task EnsureDataStorageIsReady(IServiceProvider services)
42+
{
43+
await CoreEFStartup.InitializeDatabaseAsync(services);
44+
await SimpleContentEFStartup.InitializeDatabaseAsync(services);
45+
await LoggingEFStartup.InitializeDatabaseAsync(services);
46+
}
47+
}

Server/Data/CoreEFStartup.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using Microsoft.Extensions.DependencyInjection;
4+
5+
namespace AspCoreServer.Data
6+
{
7+
public static class CoreEFStartup
8+
{
9+
public static async Task InitializeDatabaseAsync(IServiceProvider services)
10+
{
11+
var context = services.GetRequiredService<SpaDbContext>();
12+
13+
await context.Database.EnsureCreatedAsync();
14+
}
15+
16+
}
17+
}

Server/Data/LoggingEFStartup.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
4+
namespace AspCoreServer.Data
5+
{
6+
public static class LoggingEFStartup
7+
{
8+
public static async Task InitializeDatabaseAsync(IServiceProvider services)
9+
{
10+
//Implent to your hearts' content
11+
}
12+
}
13+
}
Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
using System;
2-
using System.Linq;
3-
using AspCoreServer;
2+
using System.Threading.Tasks;
43
using AspCoreServer.Models;
54
using Microsoft.EntityFrameworkCore;
65
using Microsoft.Extensions.DependencyInjection;
76

8-
namespace AspCoreServer.Data {
9-
public static class DbInitializer {
10-
public static void Initialize (SpaDbContext context) {
11-
context.Database.EnsureCreated ();
7+
namespace AspCoreServer.Data
8+
{
9+
public static class SimpleContentEFStartup
10+
{
11+
public static async Task InitializeDatabaseAsync(IServiceProvider services)
12+
{
13+
var context = services.GetRequiredService<SpaDbContext>();
1214

13-
if (context.User.Any ()) {
15+
16+
if (await context.User.AnyAsync())
17+
{
1418
return; // DB has been seeded
1519
}
1620
var users = new User[] {
@@ -27,11 +31,9 @@ public static void Initialize (SpaDbContext context) {
2731
new User () { Name = "Gaulomatic" },
2832
new User () { Name = "GRIMMR3AP3R" }
2933
};
34+
await context.User.AddRangeAsync(users);
3035

31-
foreach (var s in users) {
32-
context.User.Add (s);
33-
}
34-
context.SaveChanges ();
36+
await context.SaveChangesAsync();
3537
}
3638
}
3739
}

0 commit comments

Comments
 (0)