Merge pull request #7526 from abpframework/dbcontext-tenancy-side

Infrastructure to allow to not create host-only tables in a tenant-only database.
pull/7528/head
Halil İbrahim Kalkan 5 years ago committed by GitHub
commit f6f29cb873
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,10 +1,72 @@
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.MultiTenancy;
namespace Microsoft.EntityFrameworkCore
{
public static class AbpModelBuilderExtensions
{
private const string ModelDatabaseProviderAnnotationKey = "_Abp_DatabaseProvider";
private const string ModelMultiTenancySideAnnotationKey = "_Abp_MultiTenancySide";
#region MultiTenancySide
public static void SetMultiTenancySide(
this ModelBuilder modelBuilder,
MultiTenancySides side)
{
modelBuilder.Model.SetAnnotation(ModelMultiTenancySideAnnotationKey, side);
}
public static MultiTenancySides GetMultiTenancySide(this ModelBuilder modelBuilder)
{
var value = modelBuilder.Model[ModelMultiTenancySideAnnotationKey];
if (value == null)
{
return MultiTenancySides.Both;
}
return (MultiTenancySides) value;
}
/// <summary>
/// Returns true if this is a database schema that is used by the host
/// but can also be shared with the tenants.
/// </summary>
public static bool IsHostDatabase(this ModelBuilder modelBuilder)
{
return modelBuilder.GetMultiTenancySide().HasFlag(MultiTenancySides.Host);
}
/// <summary>
/// Returns true if this is a database schema that is used by the tenants
/// but can also be shared with the host.
/// </summary>
public static bool IsTenantDatabase(this ModelBuilder modelBuilder)
{
return modelBuilder.GetMultiTenancySide().HasFlag(MultiTenancySides.Tenant);
}
/// <summary>
/// Returns true if this is a database schema that is only used by the host
/// and should not contain tenant-only tables.
/// </summary>
public static bool IsHostOnlyDatabase(this ModelBuilder modelBuilder)
{
return modelBuilder.GetMultiTenancySide() == MultiTenancySides.Host;
}
/// <summary>
/// Returns true if this is a database schema that is only used by tenants.
/// and should not contain host-only tables.
/// </summary>
public static bool IsTenantOnlyDatabase(this ModelBuilder modelBuilder)
{
return modelBuilder.GetMultiTenancySide() == MultiTenancySides.Tenant;
}
#endregion
#region DatabaseProvider
public static void SetDatabaseProvider(
this ModelBuilder modelBuilder,
@ -91,5 +153,7 @@ namespace Microsoft.EntityFrameworkCore
{
return modelBuilder.GetDatabaseProvider() == EfCoreDatabaseProvider.Firebird;
}
#endregion
}
}

@ -12,6 +12,11 @@ namespace Volo.Abp.BackgroundJobs.EntityFrameworkCore
{
Check.NotNull(builder, nameof(builder));
if (builder.IsTenantOnlyDatabase())
{
return;
}
var options = new BackgroundJobsModelBuilderConfigurationOptions(
BackgroundJobsDbProperties.DbTablePrefix,
BackgroundJobsDbProperties.DbSchema

@ -20,6 +20,11 @@ namespace Volo.Blogging.EntityFrameworkCore
{
Check.NotNull(builder, nameof(builder));
if (builder.IsTenantOnlyDatabase())
{
return;
}
var options = new BloggingModelBuilderConfigurationOptions(
BloggingDbProperties.DbTablePrefix,
BloggingDbProperties.DbSchema

@ -16,6 +16,11 @@ namespace Volo.Docs.EntityFrameworkCore
{
Check.NotNull(builder, nameof(builder));
if (builder.IsTenantOnlyDatabase())
{
return;
}
var options = new DocsModelBuilderConfigurationOptions(
DocsDbProperties.DbTablePrefix,
DocsDbProperties.DbSchema

@ -12,6 +12,11 @@ namespace Volo.Abp.FeatureManagement.EntityFrameworkCore
{
Check.NotNull(builder, nameof(builder));
if (builder.IsTenantOnlyDatabase())
{
return;
}
var options = new FeatureManagementModelBuilderConfigurationOptions(
FeatureManagementDbProperties.DbTablePrefix,
FeatureManagementDbProperties.DbSchema

@ -152,6 +152,8 @@ namespace Volo.Abp.Identity.EntityFrameworkCore
b.HasIndex(uc => uc.RoleId);
});
if (builder.IsHostDatabase())
{
builder.Entity<IdentityClaimType>(b =>
{
b.ToTable(options.TablePrefix + "ClaimTypes", options.Schema);
@ -164,6 +166,7 @@ namespace Volo.Abp.Identity.EntityFrameworkCore
b.Property(uc => uc.RegexDescription).HasMaxLength(IdentityClaimTypeConsts.MaxRegexDescriptionLength);
b.Property(uc => uc.Description).HasMaxLength(IdentityClaimTypeConsts.MaxDescriptionLength);
});
}
builder.Entity<OrganizationUnit>(b =>
{
@ -233,6 +236,8 @@ namespace Volo.Abp.Identity.EntityFrameworkCore
b.HasIndex(x => new { x.TenantId, x.UserId });
});
if (builder.IsHostDatabase())
{
builder.Entity<IdentityLinkUser>(b =>
{
b.ToTable(options.TablePrefix + "LinkUsers", options.Schema);
@ -247,8 +252,7 @@ namespace Volo.Abp.Identity.EntityFrameworkCore
LinkedTenantId = x.TargetTenantId
}).IsUnique();
});
}
}
}
}

@ -19,6 +19,11 @@ namespace Volo.Abp.IdentityServer.EntityFrameworkCore
{
Check.NotNull(builder, nameof(builder));
if (builder.IsTenantOnlyDatabase())
{
return;
}
var options = new IdentityServerModelBuilderConfigurationOptions(
AbpIdentityServerDbProperties.DbTablePrefix,
AbpIdentityServerDbProperties.DbSchema

@ -27,6 +27,11 @@ namespace Volo.Abp.SettingManagement.EntityFrameworkCore
{
Check.NotNull(builder, nameof(builder));
if (builder.IsTenantOnlyDatabase())
{
return;
}
var options = new SettingManagementModelBuilderConfigurationOptions(
AbpSettingManagementDbProperties.DbTablePrefix,
AbpSettingManagementDbProperties.DbSchema

@ -13,6 +13,11 @@ namespace Volo.Abp.TenantManagement.EntityFrameworkCore
{
Check.NotNull(builder, nameof(builder));
if (builder.IsTenantOnlyDatabase())
{
return;
}
var options = new AbpTenantManagementModelBuilderConfigurationOptions(
AbpTenantManagementDbProperties.DbTablePrefix,
AbpTenantManagementDbProperties.DbSchema

Loading…
Cancel
Save