Skip to content

Commit

Permalink
added data access layer project to solution
Browse files Browse the repository at this point in the history
  • Loading branch information
alimon808 committed Aug 8, 2017
1 parent ef39b3c commit ebf75a6
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 1 deletion.
16 changes: 16 additions & 0 deletions ContosoUniversity.Data/ApplicationContext.cs
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)
{

}
}
}
56 changes: 56 additions & 0 deletions ContosoUniversity.Data/ApplicationContextFactory.cs
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);
}
}
}
29 changes: 29 additions & 0 deletions ContosoUniversity.Data/ContosoUniversity.Data.csproj
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>
5 changes: 5 additions & 0 deletions ContosoUniversity.Data/settings.json
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"
}
}
8 changes: 7 additions & 1 deletion ContosoUniversity.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26430.14
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ContosoUniversity", "ContosoUniversity\ContosoUniversity.csproj", "{C450EC87-4873-4C75-97C5-92185A54A950}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ContosoUniversity", "ContosoUniversity\ContosoUniversity.csproj", "{C450EC87-4873-4C75-97C5-92185A54A950}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ContosoUniversity.Data", "ContosoUniversity.Data\ContosoUniversity.Data.csproj", "{DF2790E6-5498-4C3B-86B2-B59ADEF7776F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -15,6 +17,10 @@ Global
{C450EC87-4873-4C75-97C5-92185A54A950}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C450EC87-4873-4C75-97C5-92185A54A950}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C450EC87-4873-4C75-97C5-92185A54A950}.Release|Any CPU.Build.0 = Release|Any CPU
{DF2790E6-5498-4C3B-86B2-B59ADEF7776F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DF2790E6-5498-4C3B-86B2-B59ADEF7776F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DF2790E6-5498-4C3B-86B2-B59ADEF7776F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DF2790E6-5498-4C3B-86B2-B59ADEF7776F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit ebf75a6

Please sign in to comment.