From 38c381fafe4b19aeedaef743ccca03080e78f546 Mon Sep 17 00:00:00 2001
From: "Daniel Mackay [SSW]" <2636640+danielmackay@users.noreply.github.com>
Date: Sun, 17 Dec 2023 13:57:22 +1000
Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=2063=20migrate=20to=20use=20?=
=?UTF-8?q?docker=20db=20(#72)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
* Add Common.csproj, DbConnectionFactory.cs, and docker-compose.yml
* Add Common project and update connection string generation
* All DbContext's now use common DbContextFactory to create the connection string.
This allows us to easily switch out the server if needed.
* Tweak GitHub workflow
* Fixed build errors
* Try using linux-style paths
* Add explicit .NET 8 build target
* Fixed casing on Directory.Build.props
---
.github/workflows/dotnet.yml | 6 +-
BackingFields/ApplicationDbContext.cs | 6 +-
BufferingAndStreaming/ApplicationDbContext.cs | 6 +-
BulkDeletes/ApplicationDbContext.cs | 6 +-
BulkUpdates/ApplicationDbContext.cs | 6 +-
Common/Common.csproj | 13 +++++
Common/DbConnectionFactory.cs | 23 ++++++++
CompiledModels/ApplicationDbContext.cs | 6 +-
CompiledQueries/ApplicationDbContext.cs | 6 +-
DateOnlyTimeOnly/ApplicationDbContext.cs | 6 +-
DateOnlyTimeOnly/DateOnlyTimeOnly.csproj | 4 ++
DbContextPooling/Program.cs | 7 ++-
Directory.Build.Props | 5 --
Directory.Build.props | 8 +++
EfCoreSamples.sln | 6 ++
JsonColumns/ApplicationDbContext.cs | 8 +--
JsonColumns/Entities.cs | 4 +-
JsonColumns/Program.cs | 56 ++++++-------------
KeysetPagination/ApplicationDbContext.cs | 6 +-
MigrationBundles/ApplicationDbContext.cs | 6 +-
OwnedEntities/ApplicationDbContext.cs | 6 +-
QueryFilters/ApplicationDbContext.cs | 6 +-
Sequences/ApplicationDbContext.cs | 6 +-
ShadowProperties/ApplicationDbContext.cs | 8 +--
ShadowProperties/Entities.cs | 2 +-
SplitQueries/BloggingContext.cs | 6 +-
SplitQueries/Program.cs | 8 +--
.../Complex/ComplexDbContext.cs | 6 +-
TablePerHierarchy/Complex/ComplexDbContext.cs | 6 +-
TablePerType/Complex/ComplexDbContext.cs | 6 +-
Tags/ApplicationDbContext.cs | 6 +-
TemporalTables/ApplicationDbContext.cs | 6 +-
UnmappedTypes/ApplicationDbContext.cs | 6 +-
ValueConverters/ApplicationDbContext.cs | 6 +-
docker-compose.yml | 22 ++++++++
35 files changed, 175 insertions(+), 125 deletions(-)
create mode 100644 Common/Common.csproj
create mode 100644 Common/DbConnectionFactory.cs
delete mode 100644 Directory.Build.Props
create mode 100644 Directory.Build.props
create mode 100644 docker-compose.yml
diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml
index 8bd72f2..707ab04 100644
--- a/.github/workflows/dotnet.yml
+++ b/.github/workflows/dotnet.yml
@@ -18,9 +18,9 @@ jobs:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
- - name: Restore dependencies
- run: dotnet restore
+ with:
+ dotnet-version: 8.0.x
- name: Build
- run: dotnet build --no-restore
+ run: dotnet build
# - name: Test
# run: dotnet test --no-build --verbosity normal
diff --git a/BackingFields/ApplicationDbContext.cs b/BackingFields/ApplicationDbContext.cs
index 5c7e203..4de9bd9 100644
--- a/BackingFields/ApplicationDbContext.cs
+++ b/BackingFields/ApplicationDbContext.cs
@@ -1,4 +1,5 @@
-using Microsoft.EntityFrameworkCore;
+using Common;
+using Microsoft.EntityFrameworkCore;
namespace BackingFields;
@@ -9,8 +10,7 @@ public class ApplicationDbContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
- .UseSqlServer(
- @"Server=(localdb)\mssqllocaldb;Database=BackingFields;Trusted_Connection=True");
+ .UseSqlServer(DbConnectionFactory.Create("BackingFields"));
//optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
}
diff --git a/BufferingAndStreaming/ApplicationDbContext.cs b/BufferingAndStreaming/ApplicationDbContext.cs
index 32004a1..9b9b0aa 100644
--- a/BufferingAndStreaming/ApplicationDbContext.cs
+++ b/BufferingAndStreaming/ApplicationDbContext.cs
@@ -1,4 +1,5 @@
-using Microsoft.EntityFrameworkCore;
+using Common;
+using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
namespace BufferingAndStreaming;
@@ -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 });
}
diff --git a/BulkDeletes/ApplicationDbContext.cs b/BulkDeletes/ApplicationDbContext.cs
index 36dc3e2..974a512 100644
--- a/BulkDeletes/ApplicationDbContext.cs
+++ b/BulkDeletes/ApplicationDbContext.cs
@@ -1,4 +1,5 @@
-using Microsoft.EntityFrameworkCore;
+using Common;
+using Microsoft.EntityFrameworkCore;
namespace BulkDeletes;
@@ -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 });
}
diff --git a/BulkUpdates/ApplicationDbContext.cs b/BulkUpdates/ApplicationDbContext.cs
index 24a80b8..0bc7498 100644
--- a/BulkUpdates/ApplicationDbContext.cs
+++ b/BulkUpdates/ApplicationDbContext.cs
@@ -1,4 +1,5 @@
-using Microsoft.EntityFrameworkCore;
+using Common;
+using Microsoft.EntityFrameworkCore;
namespace BulkUpdates;
@@ -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 });
}
diff --git a/Common/Common.csproj b/Common/Common.csproj
new file mode 100644
index 0000000..2116ada
--- /dev/null
+++ b/Common/Common.csproj
@@ -0,0 +1,13 @@
+
+
+
+ net8.0
+ enable
+ enable
+
+
+
+
+
+
+
diff --git a/Common/DbConnectionFactory.cs b/Common/DbConnectionFactory.cs
new file mode 100644
index 0000000..e1ff11f
--- /dev/null
+++ b/Common/DbConnectionFactory.cs
@@ -0,0 +1,23 @@
+using Microsoft.Data.SqlClient;
+
+namespace Common;
+
+public static class DbConnectionFactory
+{
+ public static string Create(string databaseName)
+ {
+ var builder = new SqlConnectionStringBuilder
+ {
+ DataSource = "localhost",
+ InitialCatalog = databaseName,
+ UserID = "sa",
+ Password = "yourStrong(!)Password",
+ TrustServerCertificate = true,
+ MultipleActiveResultSets = true
+ };
+
+ Console.WriteLine($"Generated connection string: {builder.ConnectionString}");
+
+ return builder.ConnectionString;
+ }
+}
\ No newline at end of file
diff --git a/CompiledModels/ApplicationDbContext.cs b/CompiledModels/ApplicationDbContext.cs
index 1613004..0cf28a9 100644
--- a/CompiledModels/ApplicationDbContext.cs
+++ b/CompiledModels/ApplicationDbContext.cs
@@ -1,4 +1,5 @@
-using CompiledModels.CompiledModels;
+using Common;
+using CompiledModels.CompiledModels;
using Microsoft.EntityFrameworkCore;
namespace CompiledModels;
@@ -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 });
diff --git a/CompiledQueries/ApplicationDbContext.cs b/CompiledQueries/ApplicationDbContext.cs
index 96ac7e7..e3accd6 100644
--- a/CompiledQueries/ApplicationDbContext.cs
+++ b/CompiledQueries/ApplicationDbContext.cs
@@ -1,4 +1,5 @@
-using Microsoft.EntityFrameworkCore;
+using Common;
+using Microsoft.EntityFrameworkCore;
namespace CompiledQueries;
@@ -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 });
}
diff --git a/DateOnlyTimeOnly/ApplicationDbContext.cs b/DateOnlyTimeOnly/ApplicationDbContext.cs
index 5527366..2f30f7f 100644
--- a/DateOnlyTimeOnly/ApplicationDbContext.cs
+++ b/DateOnlyTimeOnly/ApplicationDbContext.cs
@@ -1,4 +1,5 @@
-using Microsoft.EntityFrameworkCore;
+using Common;
+using Microsoft.EntityFrameworkCore;
namespace DateOnlyTimeOnly;
@@ -9,8 +10,7 @@ public class ApplicationDbContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
- .UseSqlServer(
- @"Server=(localdb)\mssqllocaldb;Database=DateOnlyTimeOnly;Trusted_Connection=True");
+ .UseSqlServer(DbConnectionFactory.Create("DateOnlyTimeOnly"));
//optionsBuilder.LogTo(Console.WriteLine, new[] { RelationalEventId.CommandExecuted });
}
diff --git a/DateOnlyTimeOnly/DateOnlyTimeOnly.csproj b/DateOnlyTimeOnly/DateOnlyTimeOnly.csproj
index 2217c04..6efafb9 100644
--- a/DateOnlyTimeOnly/DateOnlyTimeOnly.csproj
+++ b/DateOnlyTimeOnly/DateOnlyTimeOnly.csproj
@@ -11,4 +11,8 @@
+
+
+
+
diff --git a/DbContextPooling/Program.cs b/DbContextPooling/Program.cs
index 66536ac..642e9ca 100644
--- a/DbContextPooling/Program.cs
+++ b/DbContextPooling/Program.cs
@@ -1,4 +1,5 @@
-using DbContextPooling;
+using Common;
+using DbContextPooling;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
@@ -8,7 +9,7 @@
Console.WriteLine("Pooling WITHOUT Dependency Injection");
var options = new DbContextOptionsBuilder()
- .UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=DbContextPooling;Trusted_Connection=True");
+ .UseSqlServer(DbConnectionFactory.Create("DbContextPooling"));
var factory = new PooledDbContextFactory(options.Options);
@@ -20,7 +21,7 @@
var services = new ServiceCollection();
services.AddDbContextPool(
- o => o.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=DbContextPooling;Trusted_Connection=True"));
+ o => o.UseSqlServer(DbConnectionFactory.Create("DbContextPooling")));
var sp = services.BuildServiceProvider();
diff --git a/Directory.Build.Props b/Directory.Build.Props
deleted file mode 100644
index a103e34..0000000
--- a/Directory.Build.Props
+++ /dev/null
@@ -1,5 +0,0 @@
-
-
- true
-
-
\ No newline at end of file
diff --git a/Directory.Build.props b/Directory.Build.props
new file mode 100644
index 0000000..5c249c4
--- /dev/null
+++ b/Directory.Build.props
@@ -0,0 +1,8 @@
+
+
+ true
+
+
+
+
+
diff --git a/EfCoreSamples.sln b/EfCoreSamples.sln
index 57b4c89..9d67f98 100644
--- a/EfCoreSamples.sln
+++ b/EfCoreSamples.sln
@@ -54,6 +54,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BufferingAndStreaming", "Bu
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompiledModels", "CompiledModels\CompiledModels.csproj", "{01B7FAFB-DA74-4259-B2C3-4DAC9154D8E8}"
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Common", "Common\Common.csproj", "{7047FA95-2543-4221-A749-35AE5E92B716}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -152,6 +154,10 @@ Global
{01B7FAFB-DA74-4259-B2C3-4DAC9154D8E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{01B7FAFB-DA74-4259-B2C3-4DAC9154D8E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{01B7FAFB-DA74-4259-B2C3-4DAC9154D8E8}.Release|Any CPU.Build.0 = Release|Any CPU
+ {7047FA95-2543-4221-A749-35AE5E92B716}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {7047FA95-2543-4221-A749-35AE5E92B716}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {7047FA95-2543-4221-A749-35AE5E92B716}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {7047FA95-2543-4221-A749-35AE5E92B716}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
diff --git a/JsonColumns/ApplicationDbContext.cs b/JsonColumns/ApplicationDbContext.cs
index 2b145c1..d564b88 100644
--- a/JsonColumns/ApplicationDbContext.cs
+++ b/JsonColumns/ApplicationDbContext.cs
@@ -1,16 +1,16 @@
-using Microsoft.EntityFrameworkCore;
+using Common;
+using Microsoft.EntityFrameworkCore;
namespace JsonColumns;
public class ApplicationDbContext : DbContext
{
- public DbSet Contacts { get; set; }
+ public DbSet Contacts { get; set; } = null!;
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 });
}
diff --git a/JsonColumns/Entities.cs b/JsonColumns/Entities.cs
index d69b2d0..0f9eeee 100644
--- a/JsonColumns/Entities.cs
+++ b/JsonColumns/Entities.cs
@@ -3,8 +3,8 @@
public class Contact
{
public int Id { get; set; }
- public string Name { get; set; } = null!;
- public Address? Address { get; set; }
+ public string Name { get; init; } = null!;
+ public required Address Address { get; set; } = null!;
public ICollection? Notes { get; set; }
public override string ToString()
diff --git a/JsonColumns/Program.cs b/JsonColumns/Program.cs
index 14d4508..008e537 100644
--- a/JsonColumns/Program.cs
+++ b/JsonColumns/Program.cs
@@ -8,55 +8,32 @@
var contacts = new List
{
- new Contact
+ new()
{
Name = "John Doe",
- Address = new Address
- {
- Line1 = "123 Main St.",
- Line2 = "Suite 101",
- City = "Seattle",
- State = "WA"
- },
- Notes = new List
- {
- new Note { Text = "Note 1"},
- new Note { Text = "Note 2"},
- new Note { Text = "Note 3"}
- }
+ Address = new Address { Line1 = "123 Main St.", Line2 = "Suite 101", City = "Seattle", State = "WA" },
+ Notes =
+ new List
+ {
+ new() { Text = "Note 1" }, new() { Text = "Note 2" }, new() { Text = "Note 3" }
+ }
},
- new Contact
+ new()
{
Name = "Jane Doe",
- Address = new Address
- {
- Line1 = "456 Main St.",
- Line2 = "Suite 202",
- City = "Seattle",
- State = "WA"
- },
+ Address = new Address { Line1 = "456 Main St.", Line2 = "Suite 202", City = "Seattle", State = "WA" },
Notes = new List
{
- new Note { Text = "Note 4"},
- new Note { Text = "Note 5"},
- new Note { Text = "Note 6"}
+ new() { Text = "Note 4" }, new() { Text = "Note 5" }, new() { Text = "Note 6" }
}
},
- new Contact
+ new()
{
Name = "John Smith",
- Address = new Address
- {
- Line1 = "789 Main St.",
- Line2 = "Suite 303",
- City = "Seattle",
- State = "WA"
- },
+ Address = new Address { Line1 = "789 Main St.", Line2 = "Suite 303", City = "Seattle", State = "WA" },
Notes = new List
{
- new Note { Text = "Note 7"},
- new Note { Text = "Note 8"},
- new Note { Text = "Note 9"}
+ new() { Text = "Note 7" }, new() { Text = "Note 8" }, new() { Text = "Note 9" }
}
}
};
@@ -72,6 +49,8 @@
.First(c => c.Name == "John Smith")
.Notes;
+ArgumentNullException.ThrowIfNull(notes);
+
foreach (var note in notes)
Console.WriteLine(note);
@@ -82,8 +61,7 @@
Console.WriteLine("Deleting Data");
var contactToDelete = db.Contacts.First(c => c.Name == "Jane Doe");
-contact.Address = null;
+contactToDelete.Address = new Address();
db.SaveChanges();
-Console.ReadLine();
-
+Console.ReadLine();
\ No newline at end of file
diff --git a/KeysetPagination/ApplicationDbContext.cs b/KeysetPagination/ApplicationDbContext.cs
index 69f6885..aaee00a 100644
--- a/KeysetPagination/ApplicationDbContext.cs
+++ b/KeysetPagination/ApplicationDbContext.cs
@@ -1,4 +1,5 @@
-using Microsoft.EntityFrameworkCore;
+using Common;
+using Microsoft.EntityFrameworkCore;
namespace KeysetPagination;
@@ -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 });
}
diff --git a/MigrationBundles/ApplicationDbContext.cs b/MigrationBundles/ApplicationDbContext.cs
index 384dd15..c627553 100644
--- a/MigrationBundles/ApplicationDbContext.cs
+++ b/MigrationBundles/ApplicationDbContext.cs
@@ -1,4 +1,5 @@
-using Microsoft.EntityFrameworkCore;
+using Common;
+using Microsoft.EntityFrameworkCore;
namespace MigrationBundles;
@@ -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 });
}
diff --git a/OwnedEntities/ApplicationDbContext.cs b/OwnedEntities/ApplicationDbContext.cs
index a8aa76b..8147f9c 100644
--- a/OwnedEntities/ApplicationDbContext.cs
+++ b/OwnedEntities/ApplicationDbContext.cs
@@ -1,4 +1,5 @@
-using Microsoft.EntityFrameworkCore;
+using Common;
+using Microsoft.EntityFrameworkCore;
namespace OwnedEntities;
@@ -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 });
}
diff --git a/QueryFilters/ApplicationDbContext.cs b/QueryFilters/ApplicationDbContext.cs
index 3a0ab0d..5f3c40f 100644
--- a/QueryFilters/ApplicationDbContext.cs
+++ b/QueryFilters/ApplicationDbContext.cs
@@ -1,4 +1,5 @@
-using Microsoft.EntityFrameworkCore;
+using Common;
+using Microsoft.EntityFrameworkCore;
namespace QueryFilters;
@@ -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 });
}
diff --git a/Sequences/ApplicationDbContext.cs b/Sequences/ApplicationDbContext.cs
index 9278531..a401f29 100644
--- a/Sequences/ApplicationDbContext.cs
+++ b/Sequences/ApplicationDbContext.cs
@@ -1,4 +1,5 @@
-using Microsoft.EntityFrameworkCore;
+using Common;
+using Microsoft.EntityFrameworkCore;
namespace Sequences;
@@ -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 });
}
diff --git a/ShadowProperties/ApplicationDbContext.cs b/ShadowProperties/ApplicationDbContext.cs
index 601a8c0..e15bbe5 100644
--- a/ShadowProperties/ApplicationDbContext.cs
+++ b/ShadowProperties/ApplicationDbContext.cs
@@ -1,16 +1,16 @@
-using Microsoft.EntityFrameworkCore;
+using Common;
+using Microsoft.EntityFrameworkCore;
namespace ShadowProperties;
public class ApplicationDbContext : DbContext
{
- public DbSet Products { get; set; }
+ public DbSet Products { get; set; } = null!;
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 });
}
diff --git a/ShadowProperties/Entities.cs b/ShadowProperties/Entities.cs
index f4fced9..a45f2e2 100644
--- a/ShadowProperties/Entities.cs
+++ b/ShadowProperties/Entities.cs
@@ -7,7 +7,7 @@ public class Product
public decimal Price { get; set; }
// As there is no FK, this will create a shadow property for the relationship
- public Customer Customer { get; set; }
+ public required Customer Customer { get; set; }
public override string ToString()
{
diff --git a/SplitQueries/BloggingContext.cs b/SplitQueries/BloggingContext.cs
index 7837c04..8d21f61 100644
--- a/SplitQueries/BloggingContext.cs
+++ b/SplitQueries/BloggingContext.cs
@@ -1,4 +1,5 @@
-using Microsoft.EntityFrameworkCore;
+using Common;
+using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Diagnostics;
@@ -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 });
diff --git a/SplitQueries/Program.cs b/SplitQueries/Program.cs
index 19a620f..a7192ab 100644
--- a/SplitQueries/Program.cs
+++ b/SplitQueries/Program.cs
@@ -94,13 +94,13 @@
db.Blogs.AddRange(blogs);
db.SaveChanges();
-db.Posts
+_ = db.Posts
.Include(b => b.Blogs)
.Include(p => p.Tags)
.TagWith("Default Single Query")
.ToList();
-db.Posts
+_ = db.Posts
.AsSplitQuery()
.Include(b => b.Blogs)
.Include(p => p.Tags)
@@ -109,13 +109,13 @@
using var db2 = new BloggingContext(true);
-db.Posts
+_ = db.Posts
.Include(b => b.Blogs)
.Include(p => p.Tags)
.TagWith("Default Split Query")
.ToList();
-db.Posts
+_ = db.Posts
.AsSingleQuery()
.Include(b => b.Blogs)
.Include(p => p.Tags)
diff --git a/TablePerConcreteType/Complex/ComplexDbContext.cs b/TablePerConcreteType/Complex/ComplexDbContext.cs
index 5f14715..8bd39e1 100644
--- a/TablePerConcreteType/Complex/ComplexDbContext.cs
+++ b/TablePerConcreteType/Complex/ComplexDbContext.cs
@@ -1,4 +1,5 @@
-using Microsoft.EntityFrameworkCore;
+using Common;
+using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
namespace TablePerConcreateType.Complex;
@@ -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 });
}
diff --git a/TablePerHierarchy/Complex/ComplexDbContext.cs b/TablePerHierarchy/Complex/ComplexDbContext.cs
index d22b494..f6db40a 100644
--- a/TablePerHierarchy/Complex/ComplexDbContext.cs
+++ b/TablePerHierarchy/Complex/ComplexDbContext.cs
@@ -1,4 +1,5 @@
-using Microsoft.EntityFrameworkCore;
+using Common;
+using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
namespace TablePerHierarchy.Complex;
@@ -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 });
}
diff --git a/TablePerType/Complex/ComplexDbContext.cs b/TablePerType/Complex/ComplexDbContext.cs
index 21e55cd..8587dd2 100644
--- a/TablePerType/Complex/ComplexDbContext.cs
+++ b/TablePerType/Complex/ComplexDbContext.cs
@@ -1,4 +1,5 @@
-using Microsoft.EntityFrameworkCore;
+using Common;
+using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
namespace TablePerType.Complex;
@@ -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 });
}
diff --git a/Tags/ApplicationDbContext.cs b/Tags/ApplicationDbContext.cs
index 3eb97f1..c538df4 100644
--- a/Tags/ApplicationDbContext.cs
+++ b/Tags/ApplicationDbContext.cs
@@ -1,4 +1,5 @@
-using Microsoft.EntityFrameworkCore;
+using Common;
+using Microsoft.EntityFrameworkCore;
namespace Tags;
@@ -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 });
}
diff --git a/TemporalTables/ApplicationDbContext.cs b/TemporalTables/ApplicationDbContext.cs
index 3a61dd9..b492f89 100644
--- a/TemporalTables/ApplicationDbContext.cs
+++ b/TemporalTables/ApplicationDbContext.cs
@@ -1,4 +1,5 @@
-using Microsoft.EntityFrameworkCore;
+using Common;
+using Microsoft.EntityFrameworkCore;
namespace TemporalTables;
@@ -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 });
}
diff --git a/UnmappedTypes/ApplicationDbContext.cs b/UnmappedTypes/ApplicationDbContext.cs
index 166f509..7ab21c7 100644
--- a/UnmappedTypes/ApplicationDbContext.cs
+++ b/UnmappedTypes/ApplicationDbContext.cs
@@ -1,4 +1,5 @@
-using Microsoft.EntityFrameworkCore;
+using Common;
+using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
namespace UnmappedTypes;
@@ -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 });
}
diff --git a/ValueConverters/ApplicationDbContext.cs b/ValueConverters/ApplicationDbContext.cs
index cbf5f0f..4939587 100644
--- a/ValueConverters/ApplicationDbContext.cs
+++ b/ValueConverters/ApplicationDbContext.cs
@@ -1,4 +1,5 @@
-using Microsoft.EntityFrameworkCore;
+using Common;
+using Microsoft.EntityFrameworkCore;
namespace ValueConverters;
@@ -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 });
}
diff --git a/docker-compose.yml b/docker-compose.yml
new file mode 100644
index 0000000..f8a1928
--- /dev/null
+++ b/docker-compose.yml
@@ -0,0 +1,22 @@
+# docker run --cap-add SYS_PTRACE -e 'ACCEPT_EULA=1' -e 'MSSQL_SA_PASSWORD=yourStrong(!)Password' -p 1433:1433 --name azuresqledge -d mcr.microsoft.com/azure-sql-edge
+
+services:
+ db:
+ environment:
+ ACCEPT_EULA: "Y"
+ SA_PASSWORD: "yourStrong(!)Password"
+ # mssql server image isn't available for arm64 architecture, so we use azure-sql instead
+ image: mcr.microsoft.com/azure-sql-edge:latest
+ #container_name: "ef-core-samples"
+ # If you really want to use MS SQL Server, uncomment the following line
+ #image: mcr.microsoft.com/mssql/server
+ ports:
+ # {{exposed}}:{{internal}} - you'll need to contain the exposed ports if you have more than one DB server running at a time
+ - 1433:1433
+ restart: always
+ healthcheck:
+ test: ["CMD-SHELL", "/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P yourStrong(!)Password -Q 'SELECT 1' || exit 1"]
+ interval: 10s
+ retries: 10
+ start_period: 10s
+ timeout: 3s