-
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.
migrated ContosoUniversity.Data project to core 2.0
- Loading branch information
Showing
4 changed files
with
75 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,80 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Design; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.Extensions.Configuration; | ||
using System; | ||
using System.IO; | ||
|
||
namespace ContosoUniversity.Data | ||
{ | ||
// modeled from https://www.benday.com/2017/02/17/ef-core-migrations-without-hard-coding-a-connection-string-using-idbcontextfactory | ||
public class ApplicationContextFactory : IDbContextFactory<ApplicationContext> | ||
public class ApplicationContextFactory : IDesignTimeDbContextFactory<ApplicationContext> | ||
{ | ||
public ApplicationContext Create() | ||
public ApplicationContext CreateDbContext(string[] args) | ||
{ | ||
var environmentName = Environment.GetEnvironmentVariable("Hosting:Environment"); | ||
var basePath = AppContext.BaseDirectory; | ||
return Create(basePath, environmentName); | ||
var config = new ConfigurationBuilder() | ||
.SetBasePath(Directory.GetCurrentDirectory()) | ||
.AddJsonFile("appsettings.json") | ||
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("Hosting:Environment")}.json", true) | ||
.AddEnvironmentVariables() | ||
.Build(); | ||
|
||
var builder = new DbContextOptionsBuilder<ApplicationContext>(); | ||
builder.UseSqlServer(config.GetConnectionString("DefaultConnection")); | ||
return new ApplicationContext(builder.Options); | ||
} | ||
|
||
private ApplicationContext Create(string basePath, string environmentName) | ||
{ | ||
var builder = new ConfigurationBuilder() | ||
.SetBasePath(basePath) | ||
.AddJsonFile("settings.json") | ||
.AddJsonFile($"settings.{environmentName}.json", true) | ||
.AddEnvironmentVariables(); | ||
//public ApplicationContext Create() | ||
//{ | ||
// var environmentName = Environment.GetEnvironmentVariable("Hosting:Environment"); | ||
// var basePath = AppContext.BaseDirectory; | ||
|
||
var config = builder.Build(); | ||
// return Create(basePath, environmentName); | ||
//} | ||
|
||
var connstr = config.GetConnectionString("DefaultConnection"); | ||
//private ApplicationContext Create(string basePath, string environmentName) | ||
//{ | ||
// var builder = new ConfigurationBuilder() | ||
// .SetBasePath(basePath) | ||
// .AddJsonFile("appsettings.json") | ||
// .AddJsonFile($"appsettings.{environmentName}.json", true) | ||
// .AddEnvironmentVariables(); | ||
|
||
if (String.IsNullOrWhiteSpace(connstr)) | ||
{ | ||
throw new InvalidOperationException("Could not find a connection string named 'DefaultConnection'"); | ||
} | ||
// var config = builder.Build(); | ||
|
||
return Create(connstr); | ||
} | ||
// var connstr = config.GetConnectionString("DefaultConnection"); | ||
|
||
private ApplicationContext Create(string connectionString) | ||
{ | ||
if (string.IsNullOrEmpty(connectionString)) | ||
{ | ||
throw new ArgumentException($"{nameof(connectionString)} is null or empty"); | ||
} | ||
// if (String.IsNullOrWhiteSpace(connstr)) | ||
// { | ||
// throw new InvalidOperationException("Could not find a connection string named 'DefaultConnection'"); | ||
// } | ||
|
||
var optionsBuilder = new DbContextOptionsBuilder<ApplicationContext>(); | ||
optionsBuilder.UseSqlServer(connectionString, x => x.MigrationsHistoryTable("Migration", "Contoso")); | ||
// return Create(connstr); | ||
//} | ||
|
||
//private ApplicationContext Create(string connectionString) | ||
//{ | ||
// if (string.IsNullOrEmpty(connectionString)) | ||
// { | ||
// throw new ArgumentException($"{nameof(connectionString)} is null or empty"); | ||
// } | ||
|
||
// var optionsBuilder = new DbContextOptionsBuilder<ApplicationContext>(); | ||
// optionsBuilder.UseSqlServer(connectionString, x => x.MigrationsHistoryTable("Migration", "Contoso")); | ||
|
||
// return new ApplicationContext(optionsBuilder.Options); | ||
//} | ||
|
||
////public ApplicationContext Create(DbContextFactoryOptions options) | ||
////{ | ||
//// return Create(options.ContentRootPath, options.EnvironmentName); | ||
////} | ||
|
||
//public ApplicationContext Create(DbContextFactoryOptions options) | ||
//{ | ||
// return Create(Directory.GetCurrentDirectory(), Environment.GetEnvironmentVariable("Hosting:Environment")); | ||
//} | ||
|
||
return new ApplicationContext(optionsBuilder.Options); | ||
} | ||
|
||
public ApplicationContext Create(DbContextFactoryOptions options) | ||
{ | ||
return Create(options.ContentRootPath, options.EnvironmentName); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,23 +1,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp1.1</TargetFramework> | ||
<TargetFramework>netcoreapp2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.2" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.2" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="1.1.2" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.2" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="1.1.2" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.2" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.1.2" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.2" /> | ||
<PackageReference Include="System.Collections.Immutable" Version="1.3.1" /> | ||
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="4.4.0" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.0" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.0.0" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" /> | ||
<PackageReference Include="System.Collections.Immutable" Version="1.4.0" /> | ||
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="4.4.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0" /> | ||
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" /> | ||
</ItemGroup> | ||
</Project> |
File renamed without changes.
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