Skip to content

Commit de0d123

Browse files
author
ReketeBravo
committed
Modified to configure database in startup.cs
1 parent e861dad commit de0d123

File tree

4 files changed

+64
-19
lines changed

4 files changed

+64
-19
lines changed

.vscode/launch.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": ".NET Core Launch (web)",
6+
"type": "coreclr",
7+
"request": "launch",
8+
"preLaunchTask": "build",
9+
"program": "${workspaceRoot}\\src\\BareMetalApi\\bin\\Debug\\netcoreapp1.1\\BareMetalApi.dll",
10+
"args": [],
11+
"cwd": "${workspaceRoot}",
12+
"stopAtEntry": false,
13+
"internalConsoleOptions": "openOnSessionStart",
14+
"launchBrowser": {
15+
"enabled": true,
16+
"args": "${auto-detect-url}",
17+
"windows": {
18+
"command": "cmd.exe",
19+
"args": "/C start ${auto-detect-url}"
20+
},
21+
"osx": {
22+
"command": "open"
23+
},
24+
"linux": {
25+
"command": "xdg-open"
26+
}
27+
},
28+
"env": {
29+
"ASPNETCORE_ENVIRONMENT": "Development"
30+
},
31+
"sourceFileMap": {
32+
"/Views": "${workspaceRoot}/Views"
33+
}
34+
},
35+
{
36+
"name": ".NET Core Attach",
37+
"type": "coreclr",
38+
"request": "attach",
39+
"processId": "${command.pickProcess}"
40+
}
41+
]
42+
}

.vscode/tasks.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"version": "0.1.0",
3+
"command": "dotnet",
4+
"isShellCommand": true,
5+
"args": [],
6+
"tasks": [
7+
{
8+
"taskName": "build",
9+
"args": [
10+
"${workspaceRoot}\\src\\BareMetalApi\\project.json"
11+
],
12+
"isBuildCommand": true,
13+
"problemMatcher": "$msCompile"
14+
}
15+
]
16+
}
Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,14 @@
1-
using System;
21
using Microsoft.EntityFrameworkCore;
32
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
43

54
namespace BareMetalApi.Models
65
{
76
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
87
{
9-
public ApplicationDbContext() : base()
10-
{ }
11-
12-
public ApplicationDbContext(DbContextOptions<DbContext> options) : base(options)
8+
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
139
{ }
1410

1511
//object name must be same as table name defined in Migrations
1612
public DbSet<BlogArticle> BlogArticles{ get; set; }
17-
18-
protected override void OnConfiguring(DbContextOptionsBuilder builder)
19-
{
20-
string dbName = Environment.GetEnvironmentVariable("POSTGRES_DB") ?? "localhost";
21-
string dbUser = Environment.GetEnvironmentVariable("POSTGRES_USER") ?? "postgres";
22-
string dbPassword = Environment.GetEnvironmentVariable("POSTGRES_PASSWORD") ?? "password";
23-
string pgConStr = string.Format("Host={0};Port=5432;Database=Blog;User ID={1};Password={2}", dbName, dbUser, dbPassword);
24-
builder.UseNpgsql(pgConStr);
25-
base.OnConfiguring(builder);
26-
}
27-
2813
}
2914
}

src/BareMetalApi/Startup.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public partial class Startup
2020
public Startup(IHostingEnvironment env)
2121
{
2222
var builder = new ConfigurationBuilder()
23-
.SetBasePath(env.ContentRootPath)
23+
.SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), @"src\BareMetalApi"))
2424
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
2525
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
2626
.AddEnvironmentVariables();
@@ -34,7 +34,9 @@ public void ConfigureServices(IServiceCollection services)
3434
{
3535
services.AddSingleton<IBlogArticleRepository, BlogArticleRepository>();
3636

37-
services.AddDbContext<ApplicationDbContext>();
37+
//Gets connection string from appsettings.json
38+
services.AddDbContext<ApplicationDbContext>(
39+
                opts => opts.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));
3840

3941
services.AddIdentity<ApplicationUser, IdentityRole>()
4042
.AddEntityFrameworkStores<ApplicationDbContext>()
@@ -60,7 +62,7 @@ public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
6062
//Identity
6163
app.UseIdentity();
6264

63-
//Authentication
65+
//JWT
6466
ConfigureAuth(app);
6567

6668
app.UseMvc();

0 commit comments

Comments
 (0)