diff --git a/src/Finbuckle.MultiTenant.EntityFrameworkCore/MultiTenantDbContext.cs b/src/Finbuckle.MultiTenant.EntityFrameworkCore/MultiTenantDbContext.cs index aed3304a..5da8cf96 100644 --- a/src/Finbuckle.MultiTenant.EntityFrameworkCore/MultiTenantDbContext.cs +++ b/src/Finbuckle.MultiTenant.EntityFrameworkCore/MultiTenantDbContext.cs @@ -21,6 +21,18 @@ public abstract class MultiTenantDbContext : DbContext, IMultiTenantDbContext /// public TenantNotSetMode TenantNotSetMode { get; set; } = TenantNotSetMode.Throw; + /// + /// Creates a new instance of a multitenant context that accepts a IMultiTenantContextAccessor instance and an optional DbContextOptions instance. + /// + /// The tenant information to bind to the context. + /// The TContext implementation type. + /// The ITenantInfo implementation type. + /// + public static TContext Create(TTenantInfo? tenantInfo) + where TContext : DbContext + where TTenantInfo : class, ITenantInfo, new() + => Create(tenantInfo, null); + /// /// Creates a new instance of a multitenant context that accepts a IMultiTenantContextAccessor instance and an optional DbContextOptions instance. /// diff --git a/src/Finbuckle.MultiTenant.EntityFrameworkCore/MultiTenantIdentityDbContext.cs b/src/Finbuckle.MultiTenant.EntityFrameworkCore/MultiTenantIdentityDbContext.cs index 2d247a05..edfdbc61 100644 --- a/src/Finbuckle.MultiTenant.EntityFrameworkCore/MultiTenantIdentityDbContext.cs +++ b/src/Finbuckle.MultiTenant.EntityFrameworkCore/MultiTenantIdentityDbContext.cs @@ -1,9 +1,6 @@ // Copyright Finbuckle LLC, Andrew White, and Contributors. // Refer to the solution LICENSE file for more information. -using System; -using System.Threading; -using System.Threading.Tasks; using Finbuckle.MultiTenant.Abstractions; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; @@ -24,20 +21,10 @@ public MultiTenantIdentityDbContext(IMultiTenantContextAccessor multiTenantConte { } - /// - public MultiTenantIdentityDbContext(ITenantInfo tenantInfo) : base(tenantInfo) - { - } - /// public MultiTenantIdentityDbContext(IMultiTenantContextAccessor multiTenantContextAccessor, DbContextOptions options) : base(multiTenantContextAccessor, options) { } - - /// - public MultiTenantIdentityDbContext(ITenantInfo tenantInfo, DbContextOptions options) : base(tenantInfo, options) - { - } /// protected override void OnModelCreating(ModelBuilder builder) @@ -63,20 +50,10 @@ protected MultiTenantIdentityDbContext(IMultiTenantContextAccessor multiTenantCo { } - /// - protected MultiTenantIdentityDbContext(ITenantInfo tenantInfo) : base(tenantInfo) - { - } - /// protected MultiTenantIdentityDbContext(IMultiTenantContextAccessor multiTenantContextAccessor, DbContextOptions options) : base(multiTenantContextAccessor, options) { } - - /// - protected MultiTenantIdentityDbContext(ITenantInfo tenantInfo, DbContextOptions options) : base(tenantInfo, options) - { - } /// protected override void OnModelCreating(ModelBuilder builder) @@ -104,20 +81,10 @@ protected MultiTenantIdentityDbContext(IMultiTenantContextAccessor multiTenantCo { } - /// - protected MultiTenantIdentityDbContext(ITenantInfo tenantInfo) : base(tenantInfo) - { - } - /// protected MultiTenantIdentityDbContext(IMultiTenantContextAccessor multiTenantContextAccessor, DbContextOptions options) : base(multiTenantContextAccessor, options) { } - - /// - protected MultiTenantIdentityDbContext(ITenantInfo tenantInfo, DbContextOptions options) : base(tenantInfo, options) - { - } /// protected override void OnModelCreating(ModelBuilder builder) @@ -164,11 +131,6 @@ protected MultiTenantIdentityDbContext(IMultiTenantContextAccessor multiTenantCo TenantInfo = multiTenantContextAccessor.MultiTenantContext.TenantInfo; } - protected MultiTenantIdentityDbContext(ITenantInfo tenantInfo) - { - TenantInfo = tenantInfo; - } - /// /// Constructs the database context instance and binds to the current tenant. /// @@ -179,11 +141,6 @@ protected MultiTenantIdentityDbContext(IMultiTenantContextAccessor multiTenantCo TenantInfo = multiTenantContextAccessor.MultiTenantContext.TenantInfo; } - protected MultiTenantIdentityDbContext(ITenantInfo tenantInfo, DbContextOptions options) : base(options) - { - TenantInfo = tenantInfo; - } - /// protected override void OnModelCreating(ModelBuilder builder) { diff --git a/test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/Extensions/EntityTypeBuilderExtensions/TestIdentityDbContext.cs b/test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/Extensions/EntityTypeBuilderExtensions/TestIdentityDbContext.cs index c2f13d7f..5cc12432 100644 --- a/test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/Extensions/EntityTypeBuilderExtensions/TestIdentityDbContext.cs +++ b/test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/Extensions/EntityTypeBuilderExtensions/TestIdentityDbContext.cs @@ -8,9 +8,13 @@ namespace Finbuckle.MultiTenant.EntityFrameworkCore.Test.Extensions.EntityTypeBu public class TestIdentityDbContext : EntityFrameworkCore.MultiTenantIdentityDbContext { - public TestIdentityDbContext(ITenantInfo tenantInfo) : base(tenantInfo) + public TestIdentityDbContext(IMultiTenantContextAccessor multiTenantContextAccessor) : base(multiTenantContextAccessor) { - } + } + + public TestIdentityDbContext(IMultiTenantContextAccessor multiTenantContextAccessor, DbContextOptions options) : base(multiTenantContextAccessor, options) + { + } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { diff --git a/test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/MultiTenantDbContext/MultiTenantDbContextShould.cs b/test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/MultiTenantDbContext/MultiTenantDbContextShould.cs index 6b2acf17..4acd8e67 100644 --- a/test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/MultiTenantDbContext/MultiTenantDbContextShould.cs +++ b/test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/MultiTenantDbContext/MultiTenantDbContextShould.cs @@ -85,7 +85,7 @@ public void WorkWithCreateNoOptions() } [Fact] - public void CreateArbitraryDbContext() + public void CreateMultiTenantIdentityDbContext() { var tenant1 = new TenantInfo { diff --git a/test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/MultiTenantIdentityDbContext/MultiTenanIdentitytDbContextShould.cs b/test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/MultiTenantIdentityDbContext/MultiTenanIdentitytDbContextShould.cs index 2982f28c..18ad1c5d 100644 --- a/test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/MultiTenantIdentityDbContext/MultiTenanIdentitytDbContextShould.cs +++ b/test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/MultiTenantIdentityDbContext/MultiTenanIdentitytDbContextShould.cs @@ -1,8 +1,6 @@ // Copyright Finbuckle LLC, Andrew White, and Contributors. // Refer to the solution LICENSE file for more information. -using System; -using System.Linq; using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.DependencyInjection; using Xunit; @@ -18,11 +16,11 @@ public void WorkWithDependencyInjection() services.AddMultiTenant(); services.AddDbContext(); var scope = services.BuildServiceProvider().CreateScope(); - + var context = scope.ServiceProvider.GetService(); Assert.NotNull(context); } - + [Fact] public void WorkWithSingleParamCtor() { @@ -32,7 +30,7 @@ public void WorkWithSingleParamCtor() Identifier = "abc", Name = "abc" }; - using var c = new TestIdentityDbContext(tenant1); + using var c = EntityFrameworkCore.MultiTenantDbContext.Create(tenant1); Assert.NotNull(c); } @@ -53,7 +51,7 @@ public void AdjustUniqueIndexes(Type entityType) Identifier = "abc", Name = "abc" }; - using var c = new TestIdentityDbContext(tenant1); + using var c = EntityFrameworkCore.MultiTenantDbContext.Create(tenant1); foreach (var index in c.Model.FindEntityType(entityType)!.GetIndexes().Where(i => i.IsUnique)) { @@ -61,7 +59,7 @@ public void AdjustUniqueIndexes(Type entityType) Assert.Contains("TenantId", props); } } - + [Theory] // [InlineData(typeof(IdentityUser))] // [InlineData(typeof(IdentityRole))] @@ -78,7 +76,7 @@ public void AdjustPrimaryKeys(Type entityType) Identifier = "abc", Name = "abc" }; - using var c = new TestIdentityDbContext(tenant1); + using var c = EntityFrameworkCore.MultiTenantDbContext.Create(tenant1); foreach (var key in c.Model.FindEntityType(entityType)!.GetKeys()) { @@ -103,7 +101,7 @@ public void SetMultiTenantOnIdentityDbContextVariant_None(Type entityType, bool Identifier = "abc", Name = "abc" }; - using var c = new TestIdentityDbContext(tenant1); + using var c = EntityFrameworkCore.MultiTenantDbContext.Create(tenant1); Assert.Equal(isMultiTenant, c.Model.FindEntityType(entityType).IsMultiTenant()); } @@ -124,7 +122,7 @@ public void SetMultiTenantOnIdentityDbContextVariant_TUser(Type entityType, bool Identifier = "abc", Name = "abc" }; - using var c = new TestIdentityDbContextTUser(tenant1); + using var c = EntityFrameworkCore.MultiTenantDbContext.Create(tenant1); Assert.Equal(isMultiTenant, c.Model.FindEntityType(entityType).IsMultiTenant()); } @@ -145,7 +143,8 @@ public void SetMultiTenantOnIdentityDbContextVariant_TUser_TRole(Type entityType Identifier = "abc", Name = "abc" }; - using var c = new TestIdentityDbContextTUserTRole(tenant1); + using var c = + EntityFrameworkCore.MultiTenantDbContext.Create(tenant1); Assert.Equal(isMultiTenant, c.Model.FindEntityType(entityType).IsMultiTenant()); } @@ -166,7 +165,7 @@ public void SetMultiTenantOnIdentityDbContextVariant_All(Type entityType, bool i Identifier = "abc", Name = "abc" }; - using var c = new TestIdentityDbContextAll(tenant1); + using var c = EntityFrameworkCore.MultiTenantDbContext.Create(tenant1); Assert.Equal(isMultiTenant, c.Model.FindEntityType(entityType).IsMultiTenant()); } diff --git a/test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/MultiTenantIdentityDbContext/TestIdentityDbContext.cs b/test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/MultiTenantIdentityDbContext/TestIdentityDbContext.cs index f9652e7f..6e2e4e2f 100644 --- a/test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/MultiTenantIdentityDbContext/TestIdentityDbContext.cs +++ b/test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/MultiTenantIdentityDbContext/TestIdentityDbContext.cs @@ -8,16 +8,8 @@ namespace Finbuckle.MultiTenant.EntityFrameworkCore.Test.MultiTenantIdentityDbCo public class TestIdentityDbContext : EntityFrameworkCore.MultiTenantIdentityDbContext { - public TestIdentityDbContext(TenantInfo tenantInfo) - : base(tenantInfo) - { - } - - public TestIdentityDbContext(IMultiTenantContextAccessor multiTenantContextAccessor) : base(multiTenantContextAccessor) - { - } - - public TestIdentityDbContext(ITenantInfo tenantInfo) : base(tenantInfo) + public TestIdentityDbContext(IMultiTenantContextAccessor multiTenantContextAccessor) : base( + multiTenantContextAccessor) { } @@ -25,10 +17,6 @@ public TestIdentityDbContext(IMultiTenantContextAccessor multiTenantContextAcces { } - public TestIdentityDbContext(ITenantInfo tenantInfo, DbContextOptions options) : base(tenantInfo, options) - { - } - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlite("DataSource=:memory:"); diff --git a/test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/MultiTenantIdentityDbContext/TestIdentityDbContextAll.cs b/test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/MultiTenantIdentityDbContext/TestIdentityDbContextAll.cs index 433cc051..b6ca7a55 100644 --- a/test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/MultiTenantIdentityDbContext/TestIdentityDbContextAll.cs +++ b/test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/MultiTenantIdentityDbContext/TestIdentityDbContextAll.cs @@ -1,6 +1,7 @@ // Copyright Finbuckle LLC, Andrew White, and Contributors. // Refer to the solution LICENSE file for more information. +using Finbuckle.MultiTenant.Abstractions; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; @@ -8,11 +9,13 @@ namespace Finbuckle.MultiTenant.EntityFrameworkCore.Test.MultiTenantIdentityDbCo public class TestIdentityDbContextAll : MultiTenantIdentityDbContext, IdentityUserRole, IdentityUserLogin, IdentityRoleClaim, IdentityUserToken> { + public TestIdentityDbContextAll(IMultiTenantContextAccessor multiTenantContextAccessor) : base(multiTenantContextAccessor) + { + } - public TestIdentityDbContextAll(TenantInfo tenantInfo) - : base(tenantInfo) + public TestIdentityDbContextAll(IMultiTenantContextAccessor multiTenantContextAccessor, DbContextOptions options) : base(multiTenantContextAccessor, options) { - } + } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { diff --git a/test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/MultiTenantIdentityDbContext/TestIdentityDbContextTUser.cs b/test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/MultiTenantIdentityDbContext/TestIdentityDbContextTUser.cs index c33c1fc1..026e8750 100644 --- a/test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/MultiTenantIdentityDbContext/TestIdentityDbContextTUser.cs +++ b/test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/MultiTenantIdentityDbContext/TestIdentityDbContextTUser.cs @@ -1,6 +1,7 @@ // Copyright Finbuckle LLC, Andrew White, and Contributors. // Refer to the solution LICENSE file for more information. +using Finbuckle.MultiTenant.Abstractions; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; @@ -8,10 +9,13 @@ namespace Finbuckle.MultiTenant.EntityFrameworkCore.Test.MultiTenantIdentityDbCo public class TestIdentityDbContextTUser : MultiTenantIdentityDbContext { - public TestIdentityDbContextTUser(TenantInfo tenantInfo) - : base(tenantInfo) + public TestIdentityDbContextTUser(IMultiTenantContextAccessor multiTenantContextAccessor) : base(multiTenantContextAccessor) { - } + } + + public TestIdentityDbContextTUser(IMultiTenantContextAccessor multiTenantContextAccessor, DbContextOptions options) : base(multiTenantContextAccessor, options) + { + } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { diff --git a/test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/MultiTenantIdentityDbContext/TestIdentityDbContextTUserTRole.cs b/test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/MultiTenantIdentityDbContext/TestIdentityDbContextTUserTRole.cs index 45742838..5f266f8d 100644 --- a/test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/MultiTenantIdentityDbContext/TestIdentityDbContextTUserTRole.cs +++ b/test/Finbuckle.MultiTenant.EntityFrameworkCore.Test/MultiTenantIdentityDbContext/TestIdentityDbContextTUserTRole.cs @@ -1,6 +1,7 @@ // Copyright Finbuckle LLC, Andrew White, and Contributors. // Refer to the solution LICENSE file for more information. +using Finbuckle.MultiTenant.Abstractions; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; @@ -8,10 +9,13 @@ namespace Finbuckle.MultiTenant.EntityFrameworkCore.Test.MultiTenantIdentityDbCo public class TestIdentityDbContextTUserTRole : MultiTenantIdentityDbContext { - public TestIdentityDbContextTUserTRole(TenantInfo tenantInfo) - : base(tenantInfo) + public TestIdentityDbContextTUserTRole(IMultiTenantContextAccessor multiTenantContextAccessor) : base(multiTenantContextAccessor) { - } + } + + public TestIdentityDbContextTUserTRole(IMultiTenantContextAccessor multiTenantContextAccessor, DbContextOptions options) : base(multiTenantContextAccessor, options) + { + } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {