From 189a95c0e581eb7b9da9de3e48e611075d332236 Mon Sep 17 00:00:00 2001 From: "Daniel Mackay [SSW]" <2636640+danielmackay@users.noreply.github.com> Date: Sun, 17 Dec 2023 10:00:42 +1000 Subject: [PATCH 1/8] Add Common.csproj, DbConnectionFactory.cs, and docker-compose.yml --- Common/Common.csproj | 13 +++++++++++++ Common/DbConnectionFactory.cs | 19 +++++++++++++++++++ docker-compose.yml | 20 ++++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 Common/Common.csproj create mode 100644 Common/DbConnectionFactory.cs create mode 100644 docker-compose.yml 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..81df531 --- /dev/null +++ b/Common/DbConnectionFactory.cs @@ -0,0 +1,19 @@ +using Microsoft.Data.SqlClient; + +namespace Common; + +public static class DbConnectionFactory +{ + public static string Create(string databaseName) + { + var builder = new SqlConnectionStringBuilder + { + DataSource = "localhost:1433", + InitialCatalog = databaseName, + IntegratedSecurity = true, + MultipleActiveResultSets = true + }; + + return builder.ConnectionString; + } +} \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..bf44bb4 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,20 @@ +# 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 + # If you really want to use MS SQL Server, uncomment the following line + #image: mcr.microsoft.com/mssql/server + ports: + - 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 \ No newline at end of file From 8acc913e81f2d2c5434b804d555073d65202d2ec Mon Sep 17 00:00:00 2001 From: "Daniel Mackay [SSW]" <2636640+danielmackay@users.noreply.github.com> Date: Sun, 17 Dec 2023 10:00:51 +1000 Subject: [PATCH 2/8] Add Common project and update connection string generation --- BackingFields/ApplicationDbContext.cs | 7 ++++--- BackingFields/BackingFields.csproj | 4 ++++ Common/DbConnectionFactory.cs | 8 ++++++-- DateOnlyTimeOnly/ApplicationDbContext.cs | 6 +++--- DateOnlyTimeOnly/DateOnlyTimeOnly.csproj | 4 ++++ EfCoreSamples.sln | 6 ++++++ docker-compose.yml | 4 +++- 7 files changed, 30 insertions(+), 9 deletions(-) diff --git a/BackingFields/ApplicationDbContext.cs b/BackingFields/ApplicationDbContext.cs index 5c7e203..e90beed 100644 --- a/BackingFields/ApplicationDbContext.cs +++ b/BackingFields/ApplicationDbContext.cs @@ -1,4 +1,6 @@ -using Microsoft.EntityFrameworkCore; +using Common; +using Microsoft.EntityFrameworkCore; +using System.Data.Common; namespace BackingFields; @@ -9,8 +11,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/BackingFields/BackingFields.csproj b/BackingFields/BackingFields.csproj index 2217c04..6efafb9 100644 --- a/BackingFields/BackingFields.csproj +++ b/BackingFields/BackingFields.csproj @@ -11,4 +11,8 @@ + + + + diff --git a/Common/DbConnectionFactory.cs b/Common/DbConnectionFactory.cs index 81df531..e1ff11f 100644 --- a/Common/DbConnectionFactory.cs +++ b/Common/DbConnectionFactory.cs @@ -8,12 +8,16 @@ public static string Create(string databaseName) { var builder = new SqlConnectionStringBuilder { - DataSource = "localhost:1433", + DataSource = "localhost", InitialCatalog = databaseName, - IntegratedSecurity = true, + 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/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/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/docker-compose.yml b/docker-compose.yml index bf44bb4..f8a1928 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,9 +7,11 @@ services: 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: @@ -17,4 +19,4 @@ services: interval: 10s retries: 10 start_period: 10s - timeout: 3s \ No newline at end of file + timeout: 3s From de08830528982686897bd3717debb9a2b7fae3e3 Mon Sep 17 00:00:00 2001 From: "Daniel Mackay [SSW]" <2636640+danielmackay@users.noreply.github.com> Date: Sun, 17 Dec 2023 12:56:10 +1000 Subject: [PATCH 3/8] All DbContext's now use common DbContextFactory to create the connection string. This allows us to easily switch out the server if needed. --- BackingFields/ApplicationDbContext.cs | 1 - BackingFields/BackingFields.csproj | 4 ---- BufferingAndStreaming/ApplicationDbContext.cs | 6 +++--- BulkDeletes/ApplicationDbContext.cs | 6 +++--- BulkUpdates/ApplicationDbContext.cs | 6 +++--- CompiledModels/ApplicationDbContext.cs | 6 +++--- CompiledQueries/ApplicationDbContext.cs | 6 +++--- DbContextPooling/Program.cs | 7 ++++--- Directory.Build.Props | 11 +++++++---- JsonColumns/ApplicationDbContext.cs | 6 +++--- KeysetPagination/ApplicationDbContext.cs | 6 +++--- MigrationBundles/ApplicationDbContext.cs | 6 +++--- OwnedEntities/ApplicationDbContext.cs | 6 +++--- QueryFilters/ApplicationDbContext.cs | 6 +++--- Sequences/ApplicationDbContext.cs | 6 +++--- ShadowProperties/ApplicationDbContext.cs | 6 +++--- SplitQueries/BloggingContext.cs | 6 +++--- TablePerConcreteType/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 +++--- 24 files changed, 71 insertions(+), 72 deletions(-) diff --git a/BackingFields/ApplicationDbContext.cs b/BackingFields/ApplicationDbContext.cs index e90beed..4de9bd9 100644 --- a/BackingFields/ApplicationDbContext.cs +++ b/BackingFields/ApplicationDbContext.cs @@ -1,6 +1,5 @@ using Common; using Microsoft.EntityFrameworkCore; -using System.Data.Common; namespace BackingFields; diff --git a/BackingFields/BackingFields.csproj b/BackingFields/BackingFields.csproj index 6efafb9..2217c04 100644 --- a/BackingFields/BackingFields.csproj +++ b/BackingFields/BackingFields.csproj @@ -11,8 +11,4 @@ - - - - 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/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/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 index a103e34..7375ec6 100644 --- a/Directory.Build.Props +++ b/Directory.Build.Props @@ -1,5 +1,8 @@ - - true - - \ No newline at end of file + + true + + + + + diff --git a/JsonColumns/ApplicationDbContext.cs b/JsonColumns/ApplicationDbContext.cs index 2b145c1..8a7b585 100644 --- a/JsonColumns/ApplicationDbContext.cs +++ b/JsonColumns/ApplicationDbContext.cs @@ -1,4 +1,5 @@ -using Microsoft.EntityFrameworkCore; +using Common; +using Microsoft.EntityFrameworkCore; namespace JsonColumns; @@ -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 }); } 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..f812552 100644 --- a/ShadowProperties/ApplicationDbContext.cs +++ b/ShadowProperties/ApplicationDbContext.cs @@ -1,4 +1,5 @@ -using Microsoft.EntityFrameworkCore; +using Common; +using Microsoft.EntityFrameworkCore; namespace ShadowProperties; @@ -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 }); } 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/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 }); } From 8a9c31461bfd0871bde54be78795726d3d694a8d Mon Sep 17 00:00:00 2001 From: "Daniel Mackay [SSW]" <2636640+danielmackay@users.noreply.github.com> Date: Sun, 17 Dec 2023 13:00:13 +1000 Subject: [PATCH 4/8] Tweak GitHub workflow --- .github/workflows/dotnet.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index 8bd72f2..d57b012 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -18,9 +18,7 @@ jobs: - uses: actions/checkout@v3 - name: Setup .NET uses: actions/setup-dotnet@v3 - - name: Restore dependencies - run: dotnet restore - name: Build - run: dotnet build --no-restore + run: dotnet build # - name: Test # run: dotnet test --no-build --verbosity normal From 0d07e60382bce593f4bf8ec3d91970cde9b7b92a Mon Sep 17 00:00:00 2001 From: "Daniel Mackay [SSW]" <2636640+danielmackay@users.noreply.github.com> Date: Sun, 17 Dec 2023 13:11:40 +1000 Subject: [PATCH 5/8] Fixed build errors --- JsonColumns/Entities.cs | 4 +- JsonColumns/Program.cs | 56 +++++++----------------- ShadowProperties/ApplicationDbContext.cs | 2 +- ShadowProperties/Entities.cs | 2 +- SplitQueries/Program.cs | 8 ++-- 5 files changed, 25 insertions(+), 47 deletions(-) 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/ShadowProperties/ApplicationDbContext.cs b/ShadowProperties/ApplicationDbContext.cs index f812552..e15bbe5 100644 --- a/ShadowProperties/ApplicationDbContext.cs +++ b/ShadowProperties/ApplicationDbContext.cs @@ -5,7 +5,7 @@ namespace ShadowProperties; public class ApplicationDbContext : DbContext { - public DbSet Products { get; set; } + public DbSet Products { get; set; } = null!; protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { 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/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) From fc2dfd3787d9efbef5a707a8eebb5658d5905f2d Mon Sep 17 00:00:00 2001 From: "Daniel Mackay [SSW]" <2636640+danielmackay@users.noreply.github.com> Date: Sun, 17 Dec 2023 13:15:06 +1000 Subject: [PATCH 6/8] Try using linux-style paths --- Directory.Build.Props | 2 +- JsonColumns/ApplicationDbContext.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Directory.Build.Props b/Directory.Build.Props index 7375ec6..5c249c4 100644 --- a/Directory.Build.Props +++ b/Directory.Build.Props @@ -3,6 +3,6 @@ true - + diff --git a/JsonColumns/ApplicationDbContext.cs b/JsonColumns/ApplicationDbContext.cs index 8a7b585..d564b88 100644 --- a/JsonColumns/ApplicationDbContext.cs +++ b/JsonColumns/ApplicationDbContext.cs @@ -5,7 +5,7 @@ namespace JsonColumns; public class ApplicationDbContext : DbContext { - public DbSet Contacts { get; set; } + public DbSet Contacts { get; set; } = null!; protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { From 97116369e98c9d7f05ca1216c21c4f44f0dec4ac Mon Sep 17 00:00:00 2001 From: "Daniel Mackay [SSW]" <2636640+danielmackay@users.noreply.github.com> Date: Sun, 17 Dec 2023 13:39:05 +1000 Subject: [PATCH 7/8] Add explicit .NET 8 build target --- .github/workflows/dotnet.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml index d57b012..707ab04 100644 --- a/.github/workflows/dotnet.yml +++ b/.github/workflows/dotnet.yml @@ -18,6 +18,8 @@ jobs: - uses: actions/checkout@v3 - name: Setup .NET uses: actions/setup-dotnet@v3 + with: + dotnet-version: 8.0.x - name: Build run: dotnet build # - name: Test From 2bd16a49c3d374c9c136be24b2dd6e25ae618ebf Mon Sep 17 00:00:00 2001 From: "Daniel Mackay [SSW]" <2636640+danielmackay@users.noreply.github.com> Date: Sun, 17 Dec 2023 13:53:32 +1000 Subject: [PATCH 8/8] Fixed casing on Directory.Build.props --- Directory.Build.Props => Directory.Build.props | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Directory.Build.Props => Directory.Build.props (100%) diff --git a/Directory.Build.Props b/Directory.Build.props similarity index 100% rename from Directory.Build.Props rename to Directory.Build.props