Skip to content

Commit

Permalink
All DbContext's now use common DbContextFactory to create the connect…
Browse files Browse the repository at this point in the history
…ion string.

This allows us to easily switch out the server if needed.
  • Loading branch information
danielmackay committed Dec 17, 2023
1 parent 8acc913 commit de08830
Show file tree
Hide file tree
Showing 24 changed files with 71 additions and 72 deletions.
1 change: 0 additions & 1 deletion BackingFields/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Common;
using Microsoft.EntityFrameworkCore;
using System.Data.Common;

namespace BackingFields;

Expand Down
4 changes: 0 additions & 4 deletions BackingFields/BackingFields.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,4 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Common\Common.csproj" />
</ItemGroup>

</Project>
6 changes: 3 additions & 3 deletions BufferingAndStreaming/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Common;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;

namespace BufferingAndStreaming;
Expand All @@ -10,8 +11,7 @@ public class ApplicationDbContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=BufferingAndStreaming;Trusted_Connection=True");
.UseSqlServer(DbConnectionFactory.Create("BufferingAndStreaming"));

optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
}
Expand Down
6 changes: 3 additions & 3 deletions BulkDeletes/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Common;

Check failure on line 1 in BulkDeletes/ApplicationDbContext.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Common' could not be found (are you missing a using directive or an assembly reference?)
using Microsoft.EntityFrameworkCore;

namespace BulkDeletes;

Expand All @@ -9,8 +10,7 @@ public class ApplicationDbContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=BulkDeletes;Trusted_Connection=True");
.UseSqlServer(DbConnectionFactory.Create("BulkDeletes"));

//optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
}
Expand Down
6 changes: 3 additions & 3 deletions BulkUpdates/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Common;

Check failure on line 1 in BulkUpdates/ApplicationDbContext.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Common' could not be found (are you missing a using directive or an assembly reference?)
using Microsoft.EntityFrameworkCore;

namespace BulkUpdates;

Expand All @@ -9,8 +10,7 @@ public class ApplicationDbContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=BulkUpdate;Trusted_Connection=True");
.UseSqlServer(DbConnectionFactory.Create("BulkUpdate"));

//optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
}
Expand Down
6 changes: 3 additions & 3 deletions CompiledModels/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CompiledModels.CompiledModels;
using Common;
using CompiledModels.CompiledModels;
using Microsoft.EntityFrameworkCore;

namespace CompiledModels;
Expand All @@ -10,8 +11,7 @@ public class ApplicationDbContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=CompiledModels;Trusted_Connection=True")
.UseSqlServer(DbConnectionFactory.Create("CompiledModels"))
.UseModel(ApplicationDbContextModel.Instance);

//optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
Expand Down
6 changes: 3 additions & 3 deletions CompiledQueries/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Common;

Check failure on line 1 in CompiledQueries/ApplicationDbContext.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Common' could not be found (are you missing a using directive or an assembly reference?)
using Microsoft.EntityFrameworkCore;

namespace CompiledQueries;

Expand All @@ -9,8 +10,7 @@ public class ApplicationDbContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=CompiledQueries;Trusted_Connection=True");
.UseSqlServer(DbConnectionFactory.Create("CompiledQueries"));

//optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
}
Expand Down
7 changes: 4 additions & 3 deletions DbContextPooling/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DbContextPooling;
using Common;
using DbContextPooling;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -8,7 +9,7 @@
Console.WriteLine("Pooling WITHOUT Dependency Injection");

var options = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=DbContextPooling;Trusted_Connection=True");
.UseSqlServer(DbConnectionFactory.Create("DbContextPooling"));

var factory = new PooledDbContextFactory<ApplicationDbContext>(options.Options);

Expand All @@ -20,7 +21,7 @@

var services = new ServiceCollection();
services.AddDbContextPool<ApplicationDbContext>(
o => o.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=DbContextPooling;Trusted_Connection=True"));
o => o.UseSqlServer(DbConnectionFactory.Create("DbContextPooling")));

var sp = services.BuildServiceProvider();

Expand Down
11 changes: 7 additions & 4 deletions Directory.Build.Props
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<Project>
<PropertyGroup>
<TreatWarningsAsErrors Condition="'$(Configuration)' != 'Debug'">true</TreatWarningsAsErrors>
</PropertyGroup>
</Project>
<PropertyGroup>
<TreatWarningsAsErrors Condition="'$(Configuration)' != 'Debug'">true</TreatWarningsAsErrors>
</PropertyGroup>
<ItemGroup Condition="'$(MSBuildProjectName)' != 'Common'">
<ProjectReference Include="..\Common\Common.csproj"/>
</ItemGroup>
</Project>
6 changes: 3 additions & 3 deletions JsonColumns/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Common;

Check failure on line 1 in JsonColumns/ApplicationDbContext.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Common' could not be found (are you missing a using directive or an assembly reference?)
using Microsoft.EntityFrameworkCore;

namespace JsonColumns;

Expand All @@ -9,8 +10,7 @@ public class ApplicationDbContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=JsonColumns;Trusted_Connection=True");
.UseSqlServer(DbConnectionFactory.Create("JsonColumns"));

//optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
}
Expand Down
6 changes: 3 additions & 3 deletions KeysetPagination/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Common;
using Microsoft.EntityFrameworkCore;

namespace KeysetPagination;

Expand All @@ -9,8 +10,7 @@ public class ApplicationDbContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=KeysetPagination;Trusted_Connection=True");
.UseSqlServer(DbConnectionFactory.Create("KeysetPagination"));

//optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
}
Expand Down
6 changes: 3 additions & 3 deletions MigrationBundles/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Common;

Check failure on line 1 in MigrationBundles/ApplicationDbContext.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Common' could not be found (are you missing a using directive or an assembly reference?)
using Microsoft.EntityFrameworkCore;

namespace MigrationBundles;

Expand All @@ -9,8 +10,7 @@ public class ApplicationDbContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=MigrationBundles;Trusted_Connection=True");
.UseSqlServer(DbConnectionFactory.Create("MigrationBundles"));

//optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
}
Expand Down
6 changes: 3 additions & 3 deletions OwnedEntities/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Common;
using Microsoft.EntityFrameworkCore;

namespace OwnedEntities;

Expand All @@ -9,8 +10,7 @@ public class ApplicationDbContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=OwnedEntities;Trusted_Connection=True");
.UseSqlServer(DbConnectionFactory.Create("OwnedEntities"));

//optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
}
Expand Down
6 changes: 3 additions & 3 deletions QueryFilters/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Common;

Check failure on line 1 in QueryFilters/ApplicationDbContext.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Common' could not be found (are you missing a using directive or an assembly reference?)
using Microsoft.EntityFrameworkCore;

namespace QueryFilters;

Expand All @@ -9,8 +10,7 @@ public class ApplicationDbContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=QueryFilters;Trusted_Connection=True");
.UseSqlServer(DbConnectionFactory.Create("QueryFilters"));

//optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
}
Expand Down
6 changes: 3 additions & 3 deletions Sequences/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Common;
using Microsoft.EntityFrameworkCore;

namespace Sequences;

Expand All @@ -9,8 +10,7 @@ public class ApplicationDbContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=Sequences;Trusted_Connection=True");
.UseSqlServer(DbConnectionFactory.Create("Sequences"));

//optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
}
Expand Down
6 changes: 3 additions & 3 deletions ShadowProperties/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Common;
using Microsoft.EntityFrameworkCore;

namespace ShadowProperties;

Expand All @@ -9,8 +10,7 @@ public class ApplicationDbContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=ShadowProperties;Trusted_Connection=True");
.UseSqlServer(DbConnectionFactory.Create("ShadowProperties"));

//optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
}
Expand Down
6 changes: 3 additions & 3 deletions SplitQueries/BloggingContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Common;

Check failure on line 1 in SplitQueries/BloggingContext.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Common' could not be found (are you missing a using directive or an assembly reference?)
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Diagnostics;

Expand All @@ -22,8 +23,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
var splitQueryBehavior = _useSplitQueries ? QuerySplittingBehavior.SplitQuery : QuerySplittingBehavior.SingleQuery;

optionsBuilder
.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=SplitQueries;Trusted_Connection=True",
.UseSqlServer(DbConnectionFactory.Create("SplitQueries"),
options => options.UseQuerySplittingBehavior(splitQueryBehavior));

optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
Expand Down
6 changes: 3 additions & 3 deletions TablePerConcreteType/Complex/ComplexDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Common;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;

namespace TablePerConcreateType.Complex;
Expand All @@ -14,8 +15,7 @@ public class ComplexDbContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=ComplexTpc;Trusted_Connection=True");
.UseSqlServer(DbConnectionFactory.Create("ComplexTpc"));

optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
}
Expand Down
6 changes: 3 additions & 3 deletions TablePerHierarchy/Complex/ComplexDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Common;

Check failure on line 1 in TablePerHierarchy/Complex/ComplexDbContext.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Common' could not be found (are you missing a using directive or an assembly reference?)
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;

namespace TablePerHierarchy.Complex;
Expand All @@ -12,8 +13,7 @@ public class ComplexDbContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=ComplexTph;Trusted_Connection=True");
.UseSqlServer(DbConnectionFactory.Create("ComplexTph"));

optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
}
Expand Down
6 changes: 3 additions & 3 deletions TablePerType/Complex/ComplexDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Common;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;

namespace TablePerType.Complex;
Expand All @@ -14,8 +15,7 @@ public class ComplexDbContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=ComplexTpt;Trusted_Connection=True");
.UseSqlServer(DbConnectionFactory.Create("ComplexTpt"));

optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
}
Expand Down
6 changes: 3 additions & 3 deletions Tags/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Common;

Check failure on line 1 in Tags/ApplicationDbContext.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Common' could not be found (are you missing a using directive or an assembly reference?)
using Microsoft.EntityFrameworkCore;

namespace Tags;

Expand All @@ -9,8 +10,7 @@ public class ApplicationDbContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=Tags;Trusted_Connection=True");
.UseSqlServer(DbConnectionFactory.Create("Tags"));

//optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
}
Expand Down
6 changes: 3 additions & 3 deletions TemporalTables/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Common;
using Microsoft.EntityFrameworkCore;

namespace TemporalTables;

Expand All @@ -9,8 +10,7 @@ public class ApplicationDbContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=TemporalTables;Trusted_Connection=True");
.UseSqlServer(DbConnectionFactory.Create("TemporalTables"));

//optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
}
Expand Down
6 changes: 3 additions & 3 deletions UnmappedTypes/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Common;

Check failure on line 1 in UnmappedTypes/ApplicationDbContext.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'Common' could not be found (are you missing a using directive or an assembly reference?)
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;

namespace UnmappedTypes;
Expand All @@ -10,8 +11,7 @@ public class ApplicationDbContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=UnmappedTypes;Trusted_Connection=True");
.UseSqlServer(DbConnectionFactory.Create("UnmappedTypes"));

optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
}
Expand Down
6 changes: 3 additions & 3 deletions ValueConverters/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Common;
using Microsoft.EntityFrameworkCore;

namespace ValueConverters;

Expand All @@ -9,8 +10,7 @@ public class ApplicationDbContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=ValueConverters;Trusted_Connection=True");
.UseSqlServer(DbConnectionFactory.Create("ValueConverters"));

//optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
}
Expand Down

0 comments on commit de08830

Please sign in to comment.