From bdc21b6692140064b4a793912806fc2759da0273 Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 19 Nov 2026 18:47:23 +0800 Subject: [PATCH] Fix multiple typed DbContext callbacks throwing InvalidCastException --- .../AbpDbContextOptions.cs | 42 ++---- .../AbpDbContextOptions_Tests.cs | 134 ++++++++++++++++++ 2 files changed, 143 insertions(+), 33 deletions(-) create mode 100644 framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/AbpDbContextOptions_Tests.cs diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContextOptions.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContextOptions.cs index b87e9501d7..93a3cd6f6e 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContextOptions.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContextOptions.cs @@ -77,17 +77,9 @@ public class AbpDbContextOptions { Check.NotNull(action, nameof(action)); - var actions = ConventionActions.GetOrDefault(typeof(TDbContext)); - if (actions == null) - { - ConventionActions[typeof(TDbContext)] = new List - { - new Action((dbContext, builder) => action((TDbContext)dbContext, builder)) - }; - return; - } - - actions.Add(action); + ConventionActions + .GetOrAdd(typeof(TDbContext), () => new List()) + .Add(new Action((dbContext, builder) => action((TDbContext)dbContext, builder))); } public void ConfigureDefaultOnModelCreating([NotNull] Action action, bool overrideExisting = false) @@ -109,17 +101,9 @@ public class AbpDbContextOptions { Check.NotNull(action, nameof(action)); - var actions = OnModelCreatingActions.GetOrDefault(typeof(TDbContext)); - if (actions == null) - { - OnModelCreatingActions[typeof(TDbContext)] = new List - { - new Action((dbContext, builder) => action((TDbContext)dbContext, builder)) - }; - return; - } - - actions.Add(action); + OnModelCreatingActions + .GetOrAdd(typeof(TDbContext), () => new List()) + .Add(new Action((dbContext, builder) => action((TDbContext)dbContext, builder))); } public void ConfigureDefaultOnConfiguring([NotNull] Action action, bool overrideExisting = false) @@ -141,17 +125,9 @@ public class AbpDbContextOptions { Check.NotNull(action, nameof(action)); - var actions = OnConfiguringActions.GetOrDefault(typeof(TDbContext)); - if (actions == null) - { - OnConfiguringActions[typeof(TDbContext)] = new List - { - new Action((dbContext, builder) => action((TDbContext)dbContext, builder)) - }; - return; - } - - actions.Add(action); + OnConfiguringActions + .GetOrAdd(typeof(TDbContext), () => new List()) + .Add(new Action((dbContext, builder) => action((TDbContext)dbContext, builder))); } public bool IsConfiguredDefault() diff --git a/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/AbpDbContextOptions_Tests.cs b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/AbpDbContextOptions_Tests.cs new file mode 100644 index 0000000000..ca2402b85e --- /dev/null +++ b/framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/AbpDbContextOptions_Tests.cs @@ -0,0 +1,134 @@ +using System.Collections.Generic; +using System.Linq; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.Extensions.DependencyInjection; +using Shouldly; +using Xunit; + +namespace Volo.Abp.EntityFrameworkCore; + +public class AbpDbContextOptions_Tests : EntityFrameworkCoreTestBase +{ + private readonly List<(DbContext DbContext, string Name)> _executedCallbacks = new(); + + protected override void AfterAddApplication(IServiceCollection services) + { + services.AddAbpDbContext(); + services.AddAbpDbContext(); + + services.Configure(options => + { + options.ConfigureDefaultOnConfiguring((dbContext, _) => Record(dbContext, "DefaultOnConfiguring")); + options.ConfigureDefaultConvention((dbContext, _) => Record(dbContext, "DefaultConventions")); + options.ConfigureDefaultOnModelCreating((dbContext, _) => Record(dbContext, "DefaultOnModelCreating")); + + for (var i = 1; i <= 3; i++) + { + var index = i; + options.ConfigureOnConfiguring((dbContext, _) => Record(dbContext, $"OnConfiguring{index}")); + options.ConfigureConventions((dbContext, _) => Record(dbContext, $"Conventions{index}")); + options.ConfigureOnModelCreating((dbContext, _) => Record(dbContext, $"OnModelCreating{index}")); + } + + for (var i = 1; i <= 2; i++) + { + var index = i; + options.ConfigureOnConfiguring((dbContext, _) => Record(dbContext, $"SecondOnConfiguring{index}")); + options.ConfigureConventions((dbContext, _) => Record(dbContext, $"SecondConventions{index}")); + options.ConfigureOnModelCreating((dbContext, _) => Record(dbContext, $"SecondOnModelCreating{index}")); + } + }); + + base.AfterAddApplication(services); + } + + [Fact] + public void Should_Execute_All_Typed_Callbacks_In_Registration_Order() + { + var dbContext = GetRequiredService(); + + _ = dbContext.Model; + + GetExecutedCallbacks(dbContext).ShouldBe(new[] + { + "DefaultOnConfiguring", + "OnConfiguring1", + "OnConfiguring2", + "OnConfiguring3", + "DefaultConventions", + "Conventions1", + "Conventions2", + "Conventions3", + "DefaultOnModelCreating", + "OnModelCreating1", + "OnModelCreating2", + "OnModelCreating3" + }); + } + + [Fact] + public void Should_Only_Execute_Typed_Callbacks_Of_The_Same_DbContext() + { + var dbContext = GetRequiredService(); + + _ = dbContext.Model; + + GetExecutedCallbacks(dbContext).ShouldBe(new[] + { + "DefaultOnConfiguring", + "SecondOnConfiguring1", + "SecondOnConfiguring2", + "DefaultConventions", + "SecondConventions1", + "SecondConventions2", + "DefaultOnModelCreating", + "SecondOnModelCreating1", + "SecondOnModelCreating2" + }); + } + + private void Record(DbContext dbContext, string name) + { + lock (_executedCallbacks) + { + _executedCallbacks.Add((dbContext, name)); + } + } + + private List GetExecutedCallbacks(DbContext dbContext) + { + lock (_executedCallbacks) + { + return _executedCallbacks.Where(x => x.DbContext == dbContext).Select(x => x.Name).ToList(); + } + } +} + +public class CallbackTestDbContext : AbpDbContext +{ + public CallbackTestDbContext(DbContextOptions options) + : base(options) + { + } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.ReplaceService(); + base.OnConfiguring(optionsBuilder); + } +} + +public class CallbackTestSecondDbContext : AbpDbContext +{ + public CallbackTestSecondDbContext(DbContextOptions options) + : base(options) + { + } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.ReplaceService(); + base.OnConfiguring(optionsBuilder); + } +}