-
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 data access layer project to solution
- Loading branch information
Showing
5 changed files
with
113 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace ContosoUniversity.Data | ||
{ | ||
public class ApplicationContext : DbContext | ||
{ | ||
public ApplicationContext(DbContextOptions<ApplicationContext> options) : base(options) | ||
{ | ||
} | ||
|
||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
|
||
} | ||
} | ||
} |
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,56 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.Extensions.Configuration; | ||
using System; | ||
|
||
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 ApplicationContext Create() | ||
{ | ||
var environmentName = Environment.GetEnvironmentVariable("Hosting:Environment"); | ||
var basePath = AppContext.BaseDirectory; | ||
return Create(basePath, environmentName); | ||
} | ||
|
||
private ApplicationContext Create(string basePath, string environmentName) | ||
{ | ||
var builder = new ConfigurationBuilder() | ||
.SetBasePath(basePath) | ||
.AddJsonFile("settings.json") | ||
.AddJsonFile($"settings.{environmentName}.json", true) | ||
.AddEnvironmentVariables(); | ||
|
||
var config = builder.Build(); | ||
|
||
var connstr = config.GetConnectionString("DefaultConnection"); | ||
|
||
if (String.IsNullOrWhiteSpace(connstr)) | ||
{ | ||
throw new InvalidOperationException("Could not find a connection string named 'DefaultConnection'"); | ||
} | ||
|
||
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); | ||
|
||
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp1.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Remove="Entities\**" /> | ||
<EmbeddedResource Remove="Entities\**" /> | ||
<None Remove="Entities\**" /> | ||
</ItemGroup> | ||
|
||
<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" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0" /> | ||
</ItemGroup> | ||
</Project> |
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,5 @@ | ||
{ | ||
"ConnectionStrings": { | ||
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=ContosoUniversity2017;Trusted_Connection=True;MultipleActiveResultSets=true" | ||
} | ||
} |
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