forked from jasontaylordev/NorthwindTraders
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added design time DbContext factory.
Added support appsettings.Local.json (settings specific to an individuals development environment). Changed default database to LocalDb.
- Loading branch information
1 parent
df84e99
commit 5f74aa7
Showing
8 changed files
with
119 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
Northwind.Persistence/Infrastructure/DesignTimeDbContextFactoryBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Design; | ||
using Microsoft.Extensions.Configuration; | ||
using System; | ||
using System.IO; | ||
|
||
namespace Northwind.Persistence.Infrastructure | ||
{ | ||
public abstract class DesignTimeDbContextFactoryBase<TContext> : | ||
IDesignTimeDbContextFactory<TContext> where TContext : DbContext | ||
{ | ||
private const string ConnectionStringName = "NorthwindDatabase"; | ||
private const string AspNetCoreEnvironment = "ASPNETCORE_ENVIRONMENT"; | ||
|
||
public TContext CreateDbContext(string[] args) | ||
{ | ||
return Create(Directory.GetCurrentDirectory(), Environment.GetEnvironmentVariable(AspNetCoreEnvironment)); | ||
} | ||
|
||
protected abstract TContext CreateNewInstance(DbContextOptions<TContext> options); | ||
|
||
private TContext Create(string basePath, string environmentName) | ||
{ | ||
var configuration = new ConfigurationBuilder() | ||
.SetBasePath(basePath + "\\..\\Northwind..WebUI") | ||
.AddJsonFile("appsettings.json") | ||
.AddJsonFile($"appsettings.Local.json", optional: true) | ||
.AddJsonFile($"appsettings.{environmentName}.json", optional: true) | ||
.AddEnvironmentVariables() | ||
.Build(); | ||
|
||
var connectionString = configuration.GetConnectionString(ConnectionStringName); | ||
|
||
return Create(connectionString); | ||
} | ||
|
||
private TContext Create(string connectionString) | ||
{ | ||
if (string.IsNullOrEmpty(connectionString)) | ||
{ | ||
throw new ArgumentException($"Connection string '{ConnectionStringName}' is null or empty.", nameof(connectionString)); | ||
} | ||
|
||
Console.WriteLine($"DesignTimeDbContextFactoryBase.Create(string): Connection string: '{connectionString}'."); | ||
|
||
var optionsBuilder = new DbContextOptionsBuilder<TContext>(); | ||
|
||
optionsBuilder.UseSqlServer(connectionString); | ||
|
||
return CreateNewInstance(optionsBuilder.Options); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Northwind.Persistence.Infrastructure; | ||
|
||
namespace Northwind.Persistence | ||
{ | ||
public class NorthwindDbContextFactory : DesignTimeDbContextFactoryBase<NorthwindDbContext> | ||
{ | ||
protected override NorthwindDbContext CreateNewInstance(DbContextOptions<NorthwindDbContext> options) | ||
{ | ||
return new NorthwindDbContext(options); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:52467/", | ||
"sslPort": 0 | ||
} | ||
}, | ||
"profiles": { | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"Northwind.WebUI": { | ||
"commandName": "Project", | ||
"launchBrowser": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
}, | ||
"applicationUrl": "http://localhost:52468/" | ||
} | ||
} | ||
} |
Oops, something went wrong.