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 4 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,
@ -61,7 +123,7 @@ namespace Microsoft.EntityFrameworkCore
{
modelBuilder.SetDatabaseProvider(EfCoreDatabaseProvider.InMemory);
}
public static bool IsUsingInMemory(
this ModelBuilder modelBuilder)
{
@ -73,7 +135,7 @@ namespace Microsoft.EntityFrameworkCore
{
modelBuilder.SetDatabaseProvider(EfCoreDatabaseProvider.Cosmos);
}
public static bool IsUsingCosmos(
this ModelBuilder modelBuilder)
{
@ -85,11 +147,13 @@ namespace Microsoft.EntityFrameworkCore
{
modelBuilder.SetDatabaseProvider(EfCoreDatabaseProvider.Firebird);
}
public static bool IsUsingFirebird(
this ModelBuilder modelBuilder)
{
return modelBuilder.GetDatabaseProvider() == EfCoreDatabaseProvider.Firebird;
}
#endregion
}
}
}

@ -12,13 +12,18 @@ namespace Volo.Abp.BackgroundJobs.EntityFrameworkCore
{
Check.NotNull(builder, nameof(builder));
if (builder.IsTenantOnlyDatabase())
{
return;
}
var options = new BackgroundJobsModelBuilderConfigurationOptions(
BackgroundJobsDbProperties.DbTablePrefix,
BackgroundJobsDbProperties.DbSchema
);
optionsAction?.Invoke(options);
builder.Entity<BackgroundJobRecord>(b =>
{
b.ToTable(options.TablePrefix + "BackgroundJobs", options.Schema);
@ -32,9 +37,9 @@ namespace Volo.Abp.BackgroundJobs.EntityFrameworkCore
b.Property(x => x.LastTryTime);
b.Property(x => x.IsAbandoned).HasDefaultValue(false);
b.Property(x => x.Priority).HasDefaultValue(BackgroundJobPriority.Normal);
b.HasIndex(x => new { x.IsAbandoned, x.NextTryTime });
});
}
}
}
}

@ -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
@ -69,4 +74,4 @@ namespace Volo.Docs.EntityFrameworkCore
});
}
}
}
}

@ -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
@ -34,4 +39,4 @@ namespace Volo.Abp.FeatureManagement.EntityFrameworkCore
});
}
}
}
}

@ -152,18 +152,21 @@ namespace Volo.Abp.Identity.EntityFrameworkCore
b.HasIndex(uc => uc.RoleId);
});
builder.Entity<IdentityClaimType>(b =>
if (builder.IsHostDatabase())
{
b.ToTable(options.TablePrefix + "ClaimTypes", options.Schema);
builder.Entity<IdentityClaimType>(b =>
{
b.ToTable(options.TablePrefix + "ClaimTypes", options.Schema);
b.ConfigureByConvention();
b.ConfigureByConvention();
b.Property(uc => uc.Name).HasMaxLength(IdentityClaimTypeConsts.MaxNameLength)
.IsRequired(); // make unique
b.Property(uc => uc.Regex).HasMaxLength(IdentityClaimTypeConsts.MaxRegexLength);
b.Property(uc => uc.RegexDescription).HasMaxLength(IdentityClaimTypeConsts.MaxRegexDescriptionLength);
b.Property(uc => uc.Description).HasMaxLength(IdentityClaimTypeConsts.MaxDescriptionLength);
});
b.Property(uc => uc.Name).HasMaxLength(IdentityClaimTypeConsts.MaxNameLength)
.IsRequired(); // make unique
b.Property(uc => uc.Regex).HasMaxLength(IdentityClaimTypeConsts.MaxRegexLength);
b.Property(uc => uc.RegexDescription).HasMaxLength(IdentityClaimTypeConsts.MaxRegexDescriptionLength);
b.Property(uc => uc.Description).HasMaxLength(IdentityClaimTypeConsts.MaxDescriptionLength);
});
}
builder.Entity<OrganizationUnit>(b =>
{
@ -233,22 +236,23 @@ namespace Volo.Abp.Identity.EntityFrameworkCore
b.HasIndex(x => new { x.TenantId, x.UserId });
});
builder.Entity<IdentityLinkUser>(b =>
if (builder.IsHostDatabase())
{
b.ToTable(options.TablePrefix + "LinkUsers", options.Schema);
b.ConfigureByConvention();
b.HasIndex(x => new
builder.Entity<IdentityLinkUser>(b =>
{
UserId = x.SourceUserId,
TenantId = x.SourceTenantId,
LinkedUserId = x.TargetUserId,
LinkedTenantId = x.TargetTenantId
}).IsUnique();
});
b.ToTable(options.TablePrefix + "LinkUsers", options.Schema);
b.ConfigureByConvention();
b.HasIndex(x => new
{
UserId = x.SourceUserId,
TenantId = x.SourceTenantId,
LinkedUserId = x.TargetUserId,
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
@ -44,7 +49,7 @@ namespace Volo.Abp.SettingManagement.EntityFrameworkCore
if (builder.IsUsingOracle()) { SettingConsts.MaxValueLengthValue = 2000; }
b.Property(x => x.Value).HasMaxLength(SettingConsts.MaxValueLengthValue).IsRequired();
b.Property(x => x.ProviderName).HasMaxLength(SettingConsts.MaxProviderNameLength);
b.Property(x => x.ProviderKey).HasMaxLength(SettingConsts.MaxProviderKeyLength);
@ -52,4 +57,4 @@ namespace Volo.Abp.SettingManagement.EntityFrameworkCore
});
}
}
}
}

@ -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
@ -46,4 +51,4 @@ namespace Volo.Abp.TenantManagement.EntityFrameworkCore
});
}
}
}
}

Loading…
Cancel
Save