Skip to content

Commit cd4c168

Browse files
committed
Add SQL Lite with contributers migration data
1 parent df33e17 commit cd4c168

File tree

5 files changed

+91
-1
lines changed

5 files changed

+91
-1
lines changed

Asp2017.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
1010
<PackageReference Include="Microsoft.AspNetCore.SpaServices" Version="1.1.0" />
1111
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.1" />
13+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="1.1.1" />
1214
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
1315
</ItemGroup>
1416
<ItemGroup>

Server/Data/DbInitializer.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Linq;
3+
using Microsoft.EntityFrameworkCore;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using AspCoreServer.Models;
6+
using AspCoreServer;
7+
8+
namespace AspCoreServer.Data
9+
{
10+
public static class DbInitializer
11+
{
12+
public static void Initialize(SpaDbContext context)
13+
{
14+
context.Database.EnsureCreated();
15+
16+
if (context.User.Any())
17+
{
18+
return; // DB has been seeded
19+
}
20+
var users = new Users[]
21+
{
22+
new Users(){Name = "Mark Pieszak"},
23+
new Users(){Name = "Abrar Jahin"},
24+
new Users(){Name = "hakonamatata"},
25+
new Users(){Name = "LiverpoolOwen"},
26+
new Users(){Name = "Ketrex"},
27+
new Users(){Name = "markwhitfeld"},
28+
new Users(){Name = "daveo1001"},
29+
new Users(){Name = "paonath"},
30+
new Users(){Name = "nalex095"},
31+
new Users(){Name = "ORuban"},
32+
new Users(){Name = "Gaulomatic"}
33+
};
34+
35+
foreach (Users s in users)
36+
{
37+
context.User.Add(s);
38+
}
39+
context.SaveChanges();
40+
}
41+
}
42+
}

Server/Data/SpaDbContext.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using AspCoreServer.Models;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
namespace AspCoreServer.Data
5+
{
6+
public class SpaDbContext : DbContext
7+
{
8+
public SpaDbContext(DbContextOptions<SpaDbContext> options)
9+
: base(options)
10+
{
11+
Database.EnsureCreated();
12+
}
13+
14+
//List of DB Models - Add your DB models here
15+
public DbSet<Users> User { get; set; }
16+
}
17+
}

Server/Models/Users.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.ComponentModel.DataAnnotations;
3+
4+
namespace AspCoreServer.Models
5+
{
6+
public class Users
7+
{
8+
public int ID { get; set; }
9+
public string Name { get; set; }
10+
11+
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
12+
[DataType(DataType.Date)]
13+
public DateTime EntryTime { get; set; }
14+
15+
//Setting Default value
16+
public Users()
17+
{
18+
EntryTime = DateTime.Now;
19+
}
20+
}
21+
}

Startup.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
using Microsoft.Extensions.DependencyInjection;
1010
using Microsoft.Extensions.Logging;
1111
using Microsoft.AspNetCore.Http;
12+
using Microsoft.EntityFrameworkCore;
1213

1314
using Microsoft.AspNetCore.NodeServices;
15+
using AspCoreServer.Data;
1416

1517
namespace AspCoreServer
1618
{
@@ -35,10 +37,15 @@ public void ConfigureServices(IServiceCollection services)
3537
services.AddMvc();
3638

3739
services.AddNodeServices();
40+
41+
var connectionStringBuilder = new Microsoft.Data.Sqlite.SqliteConnectionStringBuilder { DataSource = "spa.db" };
42+
var connectionString = connectionStringBuilder.ToString();
43+
services.AddDbContext<SpaDbContext>(options =>
44+
options.UseSqlite(connectionString));
3845
}
3946

4047
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
41-
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
48+
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, SpaDbContext context)
4249
{
4350
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
4451
loggerFactory.AddDebug();
@@ -49,6 +56,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
4956
app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions {
5057
HotModuleReplacement = true
5158
});
59+
DbInitializer.Initialize(context);
5260
}
5361
else
5462
{

0 commit comments

Comments
 (0)