From c4cc018c77a43090d31822f92509b6de1625999a Mon Sep 17 00:00:00 2001 From: Mark J Price Date: Thu, 23 Feb 2023 16:11:36 +0000 Subject: [PATCH] Fix concurrency issues with Blazor Server projects --- .../NorthwindContextExtensions.cs | 4 +++- .../NorthwindContextExtensions.cs | 10 +++++++--- .../NorthwindContextExtensions.cs | 4 +++- .../NorthwindContextExtensions.cs | 10 +++++++--- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/vs4win/PracticalApps/Northwind.Common.DataContext.SqlServer/NorthwindContextExtensions.cs b/vs4win/PracticalApps/Northwind.Common.DataContext.SqlServer/NorthwindContextExtensions.cs index 5ac14ff..f91c486 100644 --- a/vs4win/PracticalApps/Northwind.Common.DataContext.SqlServer/NorthwindContextExtensions.cs +++ b/vs4win/PracticalApps/Northwind.Common.DataContext.SqlServer/NorthwindContextExtensions.cs @@ -23,7 +23,9 @@ public static class NorthwindContextExtensions options.LogTo(WriteLine, // Console new[] { Microsoft.EntityFrameworkCore .Diagnostics.RelationalEventId.CommandExecuting }); - }); + }, + // Register with a transient lifetime to avoid concurrency issues with Blazor Server projects. + contextLifetime: ServiceLifetime.Transient, optionsLifetime: ServiceLifetime.Transient); return services; } diff --git a/vs4win/PracticalApps/Northwind.Common.DataContext.Sqlite/NorthwindContextExtensions.cs b/vs4win/PracticalApps/Northwind.Common.DataContext.Sqlite/NorthwindContextExtensions.cs index ceb676d..8f98ab2 100644 --- a/vs4win/PracticalApps/Northwind.Common.DataContext.Sqlite/NorthwindContextExtensions.cs +++ b/vs4win/PracticalApps/Northwind.Common.DataContext.Sqlite/NorthwindContextExtensions.cs @@ -10,11 +10,13 @@ public static class NorthwindContextExtensions /// /// /// Set to override the default of ".." + /// Set to override the default of "Northwind.db" /// An IServiceCollection that can be used to add more services. public static IServiceCollection AddNorthwindContext( - this IServiceCollection services, string relativePath = "..") + this IServiceCollection services, string relativePath = "..", + string databaseFilename = "Northwind.db") { - string databasePath = Path.Combine(relativePath, "Northwind.db"); + string databasePath = Path.Combine(relativePath, databaseFilename); services.AddDbContext(options => { @@ -23,7 +25,9 @@ public static class NorthwindContextExtensions options.LogTo(WriteLine, // Console new[] { Microsoft.EntityFrameworkCore .Diagnostics.RelationalEventId.CommandExecuting }); - }); + }, + // Register with a transient lifetime to avoid concurrency issues with Blazor Server projects. + contextLifetime: ServiceLifetime.Transient, optionsLifetime: ServiceLifetime.Transient); return services; } diff --git a/vscode/PracticalApps/Northwind.Common.DataContext.SqlServer/NorthwindContextExtensions.cs b/vscode/PracticalApps/Northwind.Common.DataContext.SqlServer/NorthwindContextExtensions.cs index 5ac14ff..f91c486 100644 --- a/vscode/PracticalApps/Northwind.Common.DataContext.SqlServer/NorthwindContextExtensions.cs +++ b/vscode/PracticalApps/Northwind.Common.DataContext.SqlServer/NorthwindContextExtensions.cs @@ -23,7 +23,9 @@ public static class NorthwindContextExtensions options.LogTo(WriteLine, // Console new[] { Microsoft.EntityFrameworkCore .Diagnostics.RelationalEventId.CommandExecuting }); - }); + }, + // Register with a transient lifetime to avoid concurrency issues with Blazor Server projects. + contextLifetime: ServiceLifetime.Transient, optionsLifetime: ServiceLifetime.Transient); return services; } diff --git a/vscode/PracticalApps/Northwind.Common.DataContext.Sqlite/NorthwindContextExtensions.cs b/vscode/PracticalApps/Northwind.Common.DataContext.Sqlite/NorthwindContextExtensions.cs index ceb676d..8f98ab2 100644 --- a/vscode/PracticalApps/Northwind.Common.DataContext.Sqlite/NorthwindContextExtensions.cs +++ b/vscode/PracticalApps/Northwind.Common.DataContext.Sqlite/NorthwindContextExtensions.cs @@ -10,11 +10,13 @@ public static class NorthwindContextExtensions /// /// /// Set to override the default of ".." + /// Set to override the default of "Northwind.db" /// An IServiceCollection that can be used to add more services. public static IServiceCollection AddNorthwindContext( - this IServiceCollection services, string relativePath = "..") + this IServiceCollection services, string relativePath = "..", + string databaseFilename = "Northwind.db") { - string databasePath = Path.Combine(relativePath, "Northwind.db"); + string databasePath = Path.Combine(relativePath, databaseFilename); services.AddDbContext(options => { @@ -23,7 +25,9 @@ public static class NorthwindContextExtensions options.LogTo(WriteLine, // Console new[] { Microsoft.EntityFrameworkCore .Diagnostics.RelationalEventId.CommandExecuting }); - }); + }, + // Register with a transient lifetime to avoid concurrency issues with Blazor Server projects. + contextLifetime: ServiceLifetime.Transient, optionsLifetime: ServiceLifetime.Transient); return services; }