Exemplo n.º 1
0
        /// <summary>
        /// Register tenant shard
        /// </summary>
        /// <param name="tenantServerConfig">The tenant server configuration.</param>
        /// <param name="databaseConfig">The database configuration.</param>
        /// <param name="catalogConfig">The catalog configuration.</param>
        /// <param name="resetEventDate">If set to true, the events dates for all tenants will be reset </param>
        public async void RegisterTenantShard(TenantServerConfig tenantServerConfig, DatabaseConfig databaseConfig, CatalogConfig catalogConfig, bool resetEventDate)
        {
            //get all database in devtenantserver
            var tenants = GetAllTenantNames(tenantServerConfig, databaseConfig);

            var connectionString = new SqlConnectionStringBuilder
            {
                UserID          = databaseConfig.DatabaseUser,
                Password        = databaseConfig.DatabasePassword,
                ApplicationName = "EntityFramework",
                ConnectTimeout  = databaseConfig.ConnectionTimeOut
            };

            Shard shard = Sharding.CreateNewShard(tenantServerConfig.TenantDatabase, tenantServerConfig.TenantServer, databaseConfig.DatabaseServerPort, catalogConfig.ServicePlan);

            foreach (var tenant in tenants)
            {
                var tenantId = GetTenantKey(tenant);
                var result   = await Sharding.RegisterNewShard(tenantId, catalogConfig.ServicePlan, shard);

                if (result)
                {
                    // resets all tenants' event dates
                    if (resetEventDate)
                    {
                        #region EF6
                        //use EF6 since execution of Stored Procedure in EF Core for anonymous return type is not supported yet
                        using (var context = new TenantContext(Sharding.ShardMap, tenantId, connectionString.ConnectionString))
                        {
                            //context.Database.ExecuteSqlCommand("sp_ResetEventDates");
                        }
                        #endregion

                        #region EF core
                        //https://github.com/aspnet/EntityFramework/issues/7032
                        //using (var context = new TenantDbContext(Sharding.ShardMap, tenantId, connectionString))
                        //{
                        //    context.Database.ExecuteSqlCommand("sp_ResetEventDates");
                        //}
                        #endregion
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Register tenant shard
        /// </summary>
        /// <param name="tenantServerConfig">The tenant server configuration.</param>
        /// <param name="databaseConfig">The database configuration.</param>
        /// <param name="catalogConfig">The catalog configuration.</param>
        /// <param name="resetEventDate">If set to true, the events dates for all tenants will be reset </param>
        public async void RegisterTenantShard(TenantServerConfig tenantServerConfig, DatabaseConfig databaseConfig, CatalogConfig catalogConfig, bool resetEventDate)
        {
            //get all database in devtenantserver
            var tenants = GetAllTenantNames(tenantServerConfig, databaseConfig);

            var connectionString = new SqlConnectionStringBuilder
            {
                UserID          = databaseConfig.DatabaseUser,
                Password        = databaseConfig.DatabasePassword,
                ApplicationName = "EntityFramework",
                ConnectTimeout  = databaseConfig.ConnectionTimeOut
            };

            foreach (var tenant in tenants)
            {
                var tenantId = GetTenantKey(tenant);
                var result   = await Sharding.RegisterNewShard(tenant, tenantId, tenantServerConfig.TenantServer, databaseConfig.DatabaseServerPort, catalogConfig.ServicePlan);

                if (result)
                {
                    // resets all tenants' event dates
                    if (resetEventDate)
                    {
                        #region EF6
                        try
                        {
                            //use EF6 since execution of Stored Procedure in EF Core for anonymous return type is not supported yet
                            using (var context = new TenantContext(Sharding.ShardMap, tenantId, connectionString.ConnectionString))
                            {
                                context.Database.ExecuteSqlCommand("sp_ResetEventDates");
                            }
                        }
                        catch (Microsoft.Azure.SqlDatabase.ElasticScale.ShardManagement.ShardManagementException ex)
                        {
                            string errorText;
                            if (ex.ErrorCode == Microsoft.Azure.SqlDatabase.ElasticScale.ShardManagement.ShardManagementErrorCode.MappingIsOffline)
                            {
                                errorText = "Tenant '" + tenant + "' is offline. Could not reset event dates:" + ex.ToString();
                            }
                            else
                            {
                                errorText = ex.ToString();
                            }
                            Console.WriteLine(errorText);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.ToString());
                        }
                        #endregion

                        #region EF core
                        //https://github.com/aspnet/EntityFramework/issues/7032
                        //using (var context = new TenantDbContext(Sharding.ShardMap, tenantId, connectionString))
                        //{
                        //     context.Database.ExecuteSqlCommand("sp_ResetEventDates");
                        //}
                        #endregion
                    }
                }
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Resolves any mapping differences between the global shard map in the catalog and the local shard map located a tenant database
 /// </summary>
 /// <param name="tenantId">The tenant identifier.</param>
 /// <param name="UseGlobalShardMap">Specifies if the global shard map or the local shard map should be used as the source of truth for resolution.</param>
 public void ResolveMappingDifferences(int TenantId, bool UseGlobalShardMap = false)
 {
     Sharding.ResolveMappingDifferences(TenantId, UseGlobalShardMap);
 }