コード例 #1
0
        public static void ConfigureTenant(this ModelBuilder builder, out TenancyModelState <string> tenancyModelState)
        {
            // MultiTenancyServer configuration.
            var tenantStoreOptions = new TenantStoreOptions();

            builder.ConfigureTenantContext <Tenant, string>(tenantStoreOptions);

            // Add multi-tenancy support to model.
            var tenantReferenceOptions = new TenantReferenceOptions();

            builder.HasTenancy(tenantReferenceOptions, out tenancyModelState);
        }
 /// <summary>
 /// Configures a <see cref="ModelBuilder"/> for multi-tenancy.
 /// </summary>
 /// <typeparam name="TTenantKey">Type that represents the tenant key.</typeparam>
 /// <param name="builder">Builder describing the DbContext model.</param>
 /// <param name="options">Options describing how tenanted entities reference their owner tenant.</param>
 /// <param name="tenancyModelState">A static field on the DbContext that will store state about the multi-tenancy configuration for the context.</param>
 /// <param name="unsetTenantKey">An optional value that represents an unset tenant ID on a tenanted entity, by default this will be null for reference types and 0 for integers.</param>
 public static void HasTenancy <TTenantKey>(
     this ModelBuilder builder,
     TenantReferenceOptions options,
     out TenancyModelState <TTenantKey> tenancyModelState,
     TTenantKey unsetTenantKey = default)
     where TTenantKey : IEquatable <TTenantKey>
 {
     tenancyModelState = new TenancyModelState <TTenantKey>()
     {
         UnsetTenantKey = unsetTenantKey,
         DefaultOptions = options
     };
 }
コード例 #3
0
 /// <summary>
 /// Configures a <see cref="ModelBuilder"/> for multi-tenancy.
 /// </summary>
 /// <typeparam name="TKey">Type that represents the tenant key.</typeparam>
 /// <param name="builder">Builder describing the DbContext model.</param>
 /// <param name="options">Options describing how tenanted entities reference their owner tenant.</param>
 /// <param name="staticTenancyModelState">A static field on the DbContext that will store state about the multi-tenancy configuration for the context.</param>
 /// <param name="unsetTenantKey">An optional value that represents an unset tenant ID on a tenanted entity, by default this will be null for reference types and 0 for integers.</param>
 public static void HasTenancy <TKey>(
     this ModelBuilder builder,
     TenantReferenceOptions options,
     out object staticTenancyModelState,
     object unsetTenantKey = null)
     where TKey : IEquatable <TKey>
 {
     staticTenancyModelState = new TenancyModelState()
     {
         TenantKeyType  = typeof(TKey),
         UnsetTenantKey = unsetTenantKey ?? (typeof(TKey).IsValueType ? Activator.CreateInstance(typeof(TKey)) : null),
         DefaultOptions = options
     };
 }
コード例 #4
0
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            // Customize the ASP.NET Identity model and override the defaults if needed.
            // For example, you can rename the ASP.NET Identity table names and more.
            // Add your customizations after calling base.OnModelCreating(builder);

            // MultiTenancyServer configuration.
            var tenantStoreOptions = new TenantStoreOptions();

            builder.ConfigureTenantContext <ApplicationTenant, string>(tenantStoreOptions);

            // Add multi-tenancy support to model.
            var tenantReferenceOptions = new TenantReferenceOptions();

            builder.HasTenancy <string>(tenantReferenceOptions, out _tenancyModelState);

            // Configure custom properties on ApplicationTenant.
            builder.Entity <ApplicationTenant>(b =>
            {
                b.Property(t => t.DisplayName).HasMaxLength(256);
            });

            // Configure properties on User (ASP.NET Core Identity).
            builder.Entity <ApplicationUser>(b =>
            {
                // Add multi-tenancy support to entity.
                b.HasTenancy(() => _tenancyContext.Tenant.Id, _tenancyModelState, hasIndex: false);
                // Remove unique index on NormalizedUserName.
                b.HasIndex(u => u.NormalizedUserName).HasName("UserNameIndex").IsUnique(false);
                // Add unique index on TenantId and NormalizedUserName.
                b.HasIndex(tenantReferenceOptions.ReferenceName, nameof(ApplicationUser.NormalizedUserName))
                .HasName("TenantUserNameIndex").IsUnique();
            });

            // Configure properties on Role (ASP.NET Core Identity).
            builder.Entity <IdentityRole>(b =>
            {
                // Add multi-tenancy support to entity.
                b.HasTenancy(() => _tenancyContext.Tenant.Id, _tenancyModelState, hasIndex: false);
                // Remove unique index on NormalizedUserName.
                b.HasIndex(r => r.NormalizedName).HasName("RoleNameIndex").IsUnique(false);
                // Add unique index on TenantId and NormalizedUserName.
                b.HasIndex(tenantReferenceOptions.ReferenceName, nameof(IdentityRole.NormalizedName))
                .HasName("TenantRoleNameIndex").IsUnique();
            });
        }
コード例 #5
0
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            // Customize the ASP.NET Identity model and override the defaults if needed.
            // For example, you can rename the ASP.NET Identity table names and more.
            // Add your customizations after calling base.OnModelCreating(builder);

            var configurationStoreOptions = new ConfigurationStoreOptions();

            builder.ConfigureClientContext(configurationStoreOptions);
            builder.ConfigureResourcesContext(configurationStoreOptions);

            var operationalStoreOptions = new OperationalStoreOptions();

            builder.ConfigurePersistedGrantContext(operationalStoreOptions);

            var tenantStoreOptions = new TenantStoreOptions();

            builder.ConfigureTenantContext <ApplicationTenant, ObjectId>(tenantStoreOptions);

            var tenantReferenceOptions = new TenantReferenceOptions();

            builder.HasTenancy <string>(tenantReferenceOptions, out _tenancyModelState);

            builder.Entity <ApplicationTenant>(b =>
            {
                b.Property(t => t.DisplayName).HasMaxLength(256);
            });

            // Configure properties on User (ASP.NET Core Identity).
            builder.Entity <ApplicationUser>(b =>
            {
                // Add multi-tenancy support to entity.
                b.HasTenancy(() => _tenancyContext.Tenant.Id, _tenancyModelState, hasIndex: false);
                // Remove unique index on NormalizedUserName.
                b.HasIndex(u => u.NormalizedUserName).HasName("UserNameIndex").IsUnique(false);
                // Add unique index on TenantId and NormalizedUserName.
                b.HasIndex(tenantReferenceOptions.ReferenceName, nameof(ApplicationUser.NormalizedUserName))
                .HasName("TenantUserNameIndex").IsUnique();
            });

            // Configure properties on Role (ASP.NET Core Identity).
            builder.Entity <ApplicationRole>(b =>
            {
                // Add multi-tenancy support to entity.
                b.HasTenancy(() => _tenancyContext.Tenant.Id, _tenancyModelState, hasIndex: false);
                // Remove unique index on NormalizedName.
                b.HasIndex(r => r.NormalizedName).HasName("RoleNameIndex").IsUnique(false);
                // Add unique index on TenantId and NormalizedName.
                b.HasIndex(tenantReferenceOptions.ReferenceName, nameof(ApplicationRole.NormalizedName))
                .HasName("TenantRoleNameIndex").IsUnique();
            });

            builder.Entity <Client>(b =>
            {
                b.HasTenancy(() => _tenancyContext.Tenant.Id, _tenancyModelState, hasIndex: false);
                b.HasIndex(c => c.ClientId).IsUnique(false);
                b.HasIndex(tenantReferenceOptions.ReferenceName, nameof(Client.ClientId)).IsUnique();
            });

            builder.Entity <IdentityResource>(b =>
            {
                b.HasTenancy(() => _tenancyContext.Tenant.Id, _tenancyModelState, hasIndex: false);
                b.HasIndex(r => r.Id).IsUnique(false);
                b.HasIndex(tenantReferenceOptions.ReferenceName, nameof(IdentityResource.Id)).IsUnique();
            });

            builder.Entity <ApiResource>(b =>
            {
                b.HasTenancy(() => _tenancyContext.Tenant.Id, _tenancyModelState, hasIndex: false);
                b.HasIndex(r => r.Id).IsUnique(false);
                b.HasIndex(tenantReferenceOptions.ReferenceName, nameof(ApiResource.Id)).IsUnique();
            });

            builder.Entity <ApiScope>(b =>
            {
                b.HasTenancy(() => _tenancyContext.Tenant.Id, _tenancyModelState, hasIndex: false);
                b.HasIndex(s => s).IsUnique(false);
                b.HasIndex(tenantReferenceOptions.ReferenceName, nameof(ApiScope)).IsUnique();
            });

            builder.Entity <PersistedGrant>(b =>
            {
                b.HasTenancy(() => _tenancyContext.Tenant.Id, _tenancyModelState, indexNameFormat: $"IX_{nameof(PersistedGrant)}_{{0}}");
            });

            builder.Entity <IdentityServer4.EntityFramework.Entities.DeviceFlowCodes>(b =>
            {
                b.HasTenancy(() => _tenancyContext.Tenant.Id, _tenancyModelState, hasIndex: false);
                b.HasIndex(c => c.DeviceCode).IsUnique(false);
                b.HasIndex(tenantReferenceOptions.ReferenceName, nameof(IdentityServer4.EntityFramework.Entities.DeviceFlowCodes.DeviceCode)).IsUnique();
            });
        }