Browse Source

Merge remote-tracking branch 'origin/rel-11.0' into auto-merge-forward/rel-11.0-to-dev-207

pull/26531/head
github-actions[bot] 1 week ago
parent
commit
ffde7fcdb1
  1. 42
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContextOptions.cs
  2. 134
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/AbpDbContextOptions_Tests.cs

42
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<object>
{
new Action<DbContext, ModelConfigurationBuilder>((dbContext, builder) => action((TDbContext)dbContext, builder))
};
return;
}
actions.Add(action);
ConventionActions
.GetOrAdd(typeof(TDbContext), () => new List<object>())
.Add(new Action<DbContext, ModelConfigurationBuilder>((dbContext, builder) => action((TDbContext)dbContext, builder)));
}
public void ConfigureDefaultOnModelCreating([NotNull] Action<DbContext, ModelBuilder> 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<object>
{
new Action<DbContext, ModelBuilder>((dbContext, builder) => action((TDbContext)dbContext, builder))
};
return;
}
actions.Add(action);
OnModelCreatingActions
.GetOrAdd(typeof(TDbContext), () => new List<object>())
.Add(new Action<DbContext, ModelBuilder>((dbContext, builder) => action((TDbContext)dbContext, builder)));
}
public void ConfigureDefaultOnConfiguring([NotNull] Action<DbContext, DbContextOptionsBuilder> 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<object>
{
new Action<DbContext, DbContextOptionsBuilder>((dbContext, builder) => action((TDbContext)dbContext, builder))
};
return;
}
actions.Add(action);
OnConfiguringActions
.GetOrAdd(typeof(TDbContext), () => new List<object>())
.Add(new Action<DbContext, DbContextOptionsBuilder>((dbContext, builder) => action((TDbContext)dbContext, builder)));
}
public bool IsConfiguredDefault()

134
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<CallbackTestDbContext>();
services.AddAbpDbContext<CallbackTestSecondDbContext>();
services.Configure<AbpDbContextOptions>(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<CallbackTestDbContext>((dbContext, _) => Record(dbContext, $"OnConfiguring{index}"));
options.ConfigureConventions<CallbackTestDbContext>((dbContext, _) => Record(dbContext, $"Conventions{index}"));
options.ConfigureOnModelCreating<CallbackTestDbContext>((dbContext, _) => Record(dbContext, $"OnModelCreating{index}"));
}
for (var i = 1; i <= 2; i++)
{
var index = i;
options.ConfigureOnConfiguring<CallbackTestSecondDbContext>((dbContext, _) => Record(dbContext, $"SecondOnConfiguring{index}"));
options.ConfigureConventions<CallbackTestSecondDbContext>((dbContext, _) => Record(dbContext, $"SecondConventions{index}"));
options.ConfigureOnModelCreating<CallbackTestSecondDbContext>((dbContext, _) => Record(dbContext, $"SecondOnModelCreating{index}"));
}
});
base.AfterAddApplication(services);
}
[Fact]
public void Should_Execute_All_Typed_Callbacks_In_Registration_Order()
{
var dbContext = GetRequiredService<CallbackTestDbContext>();
_ = 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<CallbackTestSecondDbContext>();
_ = 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<string> GetExecutedCallbacks(DbContext dbContext)
{
lock (_executedCallbacks)
{
return _executedCallbacks.Where(x => x.DbContext == dbContext).Select(x => x.Name).ToList();
}
}
}
public class CallbackTestDbContext : AbpDbContext<CallbackTestDbContext>
{
public CallbackTestDbContext(DbContextOptions<CallbackTestDbContext> options)
: base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.ReplaceService<IModelCacheKeyFactory, UnitTestModelCacheKeyFactory>();
base.OnConfiguring(optionsBuilder);
}
}
public class CallbackTestSecondDbContext : AbpDbContext<CallbackTestSecondDbContext>
{
public CallbackTestSecondDbContext(DbContextOptions<CallbackTestSecondDbContext> options)
: base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.ReplaceService<IModelCacheKeyFactory, UnitTestModelCacheKeyFactory>();
base.OnConfiguring(optionsBuilder);
}
}
Loading…
Cancel
Save