protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.AddPropertyValueConversion(); var tenantId = GetTenantId(); var timeZoneName = GetTimeZoneName(); if (tenantId != 0) { modelBuilder.AddTenantFilter <int>(tenantId); } if (!string.IsNullOrEmpty(timeZoneName)) { modelBuilder.AddTimeZoneValueConversion(timeZoneName); } if (DatabaseConfig.MultiTenant.SchemaBasedMultiTenant) { var schemaInfo = TenantDbConnection.GetAsync(this.ContextAccessor.HttpContext.Request.Host.Value).Result; if (schemaInfo != null && schemaInfo.Keys.Contains(DbContext.Name)) { modelBuilder.AddSchemaBasedTenant(schemaInfo[DbContext.Name]); } } modelBuilder.GlobalQueryFilter(); //modelBuilder.Entity(typeof(vUser)).HasQueryFilter(Expression()); }
private string GetDbConnectionString(string keyName) { var connectionString = string.Empty; if (ConnectionStringConfig.ContainsKey(keyName) && !string.IsNullOrEmpty(ConnectionStringConfig[keyName])) { connectionString = ConnectionStringConfig[keyName]; } else { var hostUri = GetHostUri(); if (DatabaseConfig.MultiTenant != null && DatabaseConfig.MultiTenant.Database != null && (DatabaseConfig.MultiTenant.Database.ConfigFromDb || DatabaseConfig.MultiTenant.Database.HostUriConnectionMappings.Count > 0)) { var clientConfig = new Dictionary <string, string>(); if (DatabaseConfig.MultiTenant.Database.HostUriConnectionMappings.Count > 0 && DatabaseConfig.MultiTenant.Database.HostUriConnectionMappings.ContainsKey(hostUri)) { clientConfig = DatabaseConfig.MultiTenant.Database.HostUriConnectionMappings[hostUri]; } else { clientConfig = TenantDbConnection.GetAsync(hostUri).Result; } if (clientConfig != null && clientConfig.ContainsKey(keyName)) { connectionString = clientConfig[keyName]; } } } return(connectionString); }
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.AddPropertyValueConversion(); modelBuilder.AddCompositeKeys(); var tenantId = GetTenantId(); var timeZoneName = GetTimeZoneName(); if (tenantId != 0) { modelBuilder.AddTenantFilter <int>(tenantId); } if (!string.IsNullOrEmpty(timeZoneName)) { modelBuilder.AddTimeZoneValueConversion(timeZoneName); } if (DatabaseConfig.MultiTenant.Schema != null && (DatabaseConfig.MultiTenant.Schema.ConfigFromDb || DatabaseConfig.MultiTenant.Schema.HostUriSchemaMappings.Count > 0)) { var schemaInfo = new Dictionary <string, string>(); var hostUri = this.ContextAccessor.HttpContext.Request.Host.Value; if (DatabaseConfig.MultiTenant.Schema.ConfigFromDb) { schemaInfo = TenantDbConnection.GetAsync(hostUri).Result; } else if (DatabaseConfig.MultiTenant.Schema.HostUriSchemaMappings.ContainsKey(hostUri)) { schemaInfo = DatabaseConfig.MultiTenant.Schema.HostUriSchemaMappings[hostUri]; } if (schemaInfo != null && schemaInfo.Keys.Contains(DbContext.Name)) { modelBuilder.AddSchemaBasedTenant(schemaInfo[DbContext.Name]); } } modelBuilder.GlobalQueryFilter(); }
private string GetDbConnectionString(string keyName) { var connectionString = string.Empty; if (ConnectionStringConfig.ContainsKey(keyName) && !string.IsNullOrEmpty(ConnectionStringConfig[keyName])) { connectionString = ConnectionStringConfig[keyName]; } else { var hostUri = GetHostUri(); var clientConfig = TenantDbConnection.GetAsync(hostUri).Result; if (clientConfig != null && clientConfig.ContainsKey(keyName)) { connectionString = clientConfig[keyName]; } } return(connectionString); }