Skip to content

Commit 064395d

Browse files
committed
DB Context added
1 parent b623ad0 commit 064395d

File tree

4 files changed

+93
-36
lines changed

4 files changed

+93
-36
lines changed

DbModels/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 Angular2Spa.DbModels
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+
}

SpaDbContext.cs

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

Startup.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using Microsoft.Extensions.Configuration;
66
using Microsoft.Extensions.DependencyInjection;
77
using Microsoft.Extensions.Logging;
8+
using Microsoft.EntityFrameworkCore;
9+
using Angular2Spa.DbModels;
810

911
namespace Angular2Spa
1012
{
@@ -32,6 +34,15 @@ public Startup(IHostingEnvironment env)
3234
// This method gets called by the runtime. Use this method to add services to the container.
3335
public void ConfigureServices(IServiceCollection services)
3436
{
37+
//Monitor for performance and usage
38+
services.AddApplicationInsightsTelemetry(Configuration);
39+
40+
//Add DB Context
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));
45+
3546
// Add framework services.
3647
services.AddMvc();
3748
services.AddMemoryCache();
@@ -56,9 +67,6 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
5667
//SignalR Config
5768
app.UseSignalR();
5869

59-
//Configure monitors your live application to help you detect and diagnose performance issues and exceptions, and discover how your app is used
60-
//app.UseApplicationInsightsRequestTelemetry();
61-
6270
if (env.IsDevelopment())
6371
{
6472
app.UseDeveloperExceptionPage();
@@ -69,6 +77,9 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
6977
app.UseWebpackDevMiddleware(new WebpackDevMiddlewareOptions {
7078
HotModuleReplacement = true
7179
});
80+
81+
//Adding Seeder/Test Data
82+
AddTestData(app.ApplicationServices.GetService<SpaDbContext>());
7283
}
7384
else
7485
{
@@ -88,8 +99,17 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
8899
name: "spa-fallback",
89100
defaults: new { controller = "Home", action = "Index" });
90101
});
102+
}
103+
104+
private static void AddTestData(SpaDbContext context)
105+
{
106+
Users testUser = new Users
107+
{
108+
Name = "Abrar Jahin"
109+
};
110+
context.User.Add(testUser);
91111

112+
context.SaveChanges();
92113
}
93-
94114
}
95115
}

project.json

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
11
{
2+
"buildOptions": {
3+
"compile": {
4+
"exclude": [
5+
"node_modules",
6+
"Client"
7+
]
8+
},
9+
"debugType": "portable",
10+
"emitEntryPoint": true,
11+
"preserveCompilationContext": true
12+
},
213
"dependencies": {
314
"Gray.Microsoft.AspNetCore.SignalR.Server": "0.2.0-alpha1",
415
"Microsoft.ApplicationInsights.AspNetCore": "1.0.0",
516
"Microsoft.AspNetCore.Diagnostics": "1.1.0",
617
"Microsoft.AspNetCore.Mvc": "1.1.0",
718
"Microsoft.AspNetCore.Razor.Tools": {
8-
"version": "1.0.0-preview2-final",
9-
"type": "build"
19+
"type": "build",
20+
"version": "1.0.0-preview2-final"
1021
},
1122
"Microsoft.AspNetCore.Server.IISIntegration": "1.1.0",
1223
"Microsoft.AspNetCore.Server.Kestrel": "1.1.0",
1324
"Microsoft.AspNetCore.SpaServices": "1.1.0-*",
1425
"Microsoft.AspNetCore.StaticFiles": "1.1.0",
26+
"Microsoft.EntityFrameworkCore.Relational": "1.1.0",
27+
"Microsoft.EntityFrameworkCore.Sqlite": "1.1.0",
1528
"Microsoft.Extensions.Caching.Memory": "1.1.0",
1629
"Microsoft.Extensions.Configuration.CommandLine": "1.1.0",
1730
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0",
@@ -21,19 +34,12 @@
2134
"Microsoft.Extensions.Logging.Debug": "1.1.0",
2235
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.0",
2336
"Microsoft.NETCore.App": {
24-
"version": "1.1.0",
25-
"type": "platform"
37+
"type": "platform",
38+
"version": "1.1.0"
2639
},
2740
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.1.0",
2841
"Newtonsoft.Json": "9.0.1"
2942
},
30-
31-
"tools": {
32-
"Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
33-
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final",
34-
"Microsoft.DotNet.Watcher.Tools": "1.0.0-preview2-final"
35-
},
36-
3743
"frameworks": {
3844
"netcoreapp1.1": {
3945
"imports": [
@@ -42,25 +48,6 @@
4248
]
4349
}
4450
},
45-
46-
"buildOptions": {
47-
"debugType": "portable",
48-
"emitEntryPoint": true,
49-
"preserveCompilationContext": true,
50-
"compile": {
51-
"exclude": [
52-
"node_modules",
53-
"Client"
54-
]
55-
}
56-
},
57-
58-
"runtimeOptions": {
59-
"configProperties": {
60-
"System.GC.Server": true
61-
}
62-
},
63-
6451
"publishOptions": {
6552
"include": [
6653
"appsettings.json",
@@ -74,7 +61,11 @@
7461
"wwwroot/dist/*.map"
7562
]
7663
},
77-
64+
"runtimeOptions": {
65+
"configProperties": {
66+
"System.GC.Server": true
67+
}
68+
},
7869
"scripts": {
7970
"prepublish": [
8071
"npm install",
@@ -83,8 +74,15 @@
8374
],
8475
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
8576
},
86-
8777
"tooling": {
8878
"defaultNamespace": "Angular2Spa"
79+
},
80+
"tools": {
81+
"Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
82+
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final",
83+
"Microsoft.DotNet.Watcher.Tools": "1.0.0-preview2-final",
84+
"Microsoft.EntityFrameworkCore.Relational.Design": "1.1.0",
85+
"Microsoft.EntityFrameworkCore.Relational.Design.Specification.Tests": "1.1.0",
86+
"Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final"
8987
}
9088
}

0 commit comments

Comments
 (0)