-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
94 lines (87 loc) · 3.51 KB
/
Program.cs
File metadata and controls
94 lines (87 loc) · 3.51 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using ExpressFuncStuff.DataAccess;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace ExpressFuncStuff
{
public class Program
{
public static void Main(string[] args)
{
var host = CreateWebHostBuilder(args).Build();
SeedData(host);
host.Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseContentRoot(Directory.GetCurrentDirectory())
.ConfigureAppConfiguration((hostingContext, config) =>
{
var env = hostingContext.HostingEnvironment;
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json",
optional: true, reloadOnChange: true);
config.AddEnvironmentVariables();
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
logging.AddDebug();
logging.AddEventSourceLogger();
})
.UseStartup<Startup>();
private static void SeedData(IWebHost host)
{
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var context = services.GetRequiredService<EntityContext>();
DbInitializer.Seed(context);
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred while seeding the database.");
}
}
}
}
public static class DbInitializer
{
public static void Seed(EntityContext context)
{
context.Database.EnsureCreated();
var testBlog = context.Comments.FirstOrDefault();
if (testBlog == null)
{
context.Comments.AddRange(
new List<Comment>
{
new Comment {Id = 1, Text = "bla", PlacedAt = DateTime.Now.Subtract(new TimeSpan(1, 0, 0))},
new Comment
{
Id = 2, Text = "bla2", PlacedAt = DateTime.Now.Subtract(new TimeSpan(2, 0, 0))
},
new Comment {Id = 3, Text = "ble", PlacedAt = DateTime.Now.Subtract(new TimeSpan(2, 0, 0))},
new Comment {Id = 4, Text = "ble", PlacedAt = DateTime.Now.Subtract(new TimeSpan(4, 0, 0))},
new Comment
{
Id = 5, Text = "bled", PlacedAt = DateTime.Now.Subtract(new TimeSpan(4, 0, 0))
},
new Comment {Id = 6, Text = "bled", PlacedAt = DateTime.Now.Subtract(new TimeSpan(4, 0, 0))}
});
context.SaveChanges();
}
context.SaveChanges();
}
}
}