简体   繁体   English

如何以编程方式将sql数据库添加到弹性池?

[英]How to add a sql database to an elastic pool programmatically?

I have the following console application which creates a ShardManagerDB and creates one database for each company on the main database. 我有以下控制台应用程序,该应用程序创建一个ShardManagerDB并在主数据库上为每个公司创建一个数据库。 I can see on azure the databases created on the server however they are not on the elastic pool. 我可以看到azure在服务器上创建的数据库,但是它们不在弹性池中。

Question: 1. Is this doable with the current API? 问题:1.这对当前的API可行吗? 2. If not, what are other recommended approaches? 2.如果不是,还有哪些其他推荐方法?

using System.Data.SqlClient;
using mynm.Data;
using System.Linq;
using mynm.Models.GlobalAdmin;

namespace mynm.DbManagementTool
{
    class Program
    {
        static void Main(string[] args)
        {
            SetupSSM();
        }

        //This will create the Shard Management DB if it doesnt exist
        private static void SetupSSM()
        {
            SqlConnectionStringBuilder connStrBldr = new SqlConnectionStringBuilder
            {
                UserID = SettingsHelper.AzureUsernamedb,
                Password = SettingsHelper.AzurePasswordDb,
                ApplicationName = SettingsHelper.AzureApplicationName,
                DataSource = SettingsHelper.AzureSqlServer
            };

           DbUtils.CreateDatabaseIfNotExists(connStrBldr.ConnectionString, SettingsHelper.Azureshardmapmgrdb);
           Sharding sharding = new Sharding(SettingsHelper.AzureSqlServer, SettingsHelper.Azureshardmapmgrdb, connStrBldr.ConnectionString);
           CreateShardPerCompany(sharding);
        }

        private static void CreateShardPerCompany(Sharding sharding)
        {
            SqlConnectionStringBuilder connStrBldr = new SqlConnectionStringBuilder
            {
                UserID = SettingsHelper.AzureUsernamedb,
                Password = SettingsHelper.AzurePasswordDb,
                ApplicationName = SettingsHelper.AzureApplicationName,
                DataSource = SettingsHelper.AzureSqlServer
            };

            UnitOfWork unitOfWork = new UnitOfWork();
            ConfigurationDBDataContext context = new ConfigurationDBDataContext();
            context.Empresas.Add(new Empresa()
            {
                Id = 1,
                Nombre = "company name 1",
                NIT = "873278423",
                NombreRepresentanteLegal = "myself",
                TelefonoRepresentanteLegal = "32894823",
                NombreContacto = "myself",
                TelefonoContacto = "32423"
            });
            context.SaveChanges();

            var listofEmpresas = unitOfWork.EmpresaRepository.Get().ToList();
            foreach(Empresa empresa in listofEmpresas)
            {
                DbUtils.CreateDatabaseIfNotExists(connStrBldr.ConnectionString, empresa.NIT);
                sharding.RegisterNewShard(SettingsHelper.AzureSqlServer, empresa.NIT, connStrBldr.ConnectionString, empresa.Id);
            }
        }
    }
}

the sharding.css sharding.css

internal class Sharding


  {
        public ShardMapManager ShardMapManager { get; private set; }

        public ListShardMap<int> ShardMap { get; private set; }

        // Bootstrap Elastic Scale by creating a new shard map manager and a shard map on 
        // the shard map manager database if necessary.
        public Sharding(string smmserver, string smmdatabase, string smmconnstr)
        {
            // Connection string with administrative credentials for the root database
            SqlConnectionStringBuilder connStrBldr = new SqlConnectionStringBuilder(smmconnstr);
            connStrBldr.DataSource = smmserver;
            connStrBldr.InitialCatalog = smmdatabase;

            // Deploy shard map manager.
            ShardMapManager smm;
            if (!ShardMapManagerFactory.TryGetSqlShardMapManager(connStrBldr.ConnectionString, ShardMapManagerLoadPolicy.Lazy, out smm))
            {
                this.ShardMapManager = ShardMapManagerFactory.CreateSqlShardMapManager(connStrBldr.ConnectionString);
            }
            else
            {
                this.ShardMapManager = smm;
            }

            ListShardMap<int> sm;
            if (!ShardMapManager.TryGetListShardMap<int>("ElasticScaleWithEF", out sm))
            {
                this.ShardMap = ShardMapManager.CreateListShardMap<int>("ElasticScaleWithEF");
            }
            else
            {
                this.ShardMap = sm;
            }
        }

        // Enter a new shard - i.e. an empty database - to the shard map, allocate a first tenant to it 
        // and kick off EF intialization of the database to deploy schema
        // public void RegisterNewShard(string server, string database, string user, string pwd, string appname, int key)
        public void RegisterNewShard(string server, string database, string connstr, int key)
        {
            Shard shard;
            ShardLocation shardLocation = new ShardLocation(server, database);

            if (!this.ShardMap.TryGetShard(shardLocation, out shard))
            {
                shard = this.ShardMap.CreateShard(shardLocation);
            }

            SqlConnectionStringBuilder connStrBldr = new SqlConnectionStringBuilder(connstr);
            connStrBldr.DataSource = server;
            connStrBldr.InitialCatalog = database;

            // Go into a DbContext to trigger migrations and schema deployment for the new shard.
            // This requires an un-opened connection.

            using (var db = new ElasticScaleContext<int>(connStrBldr.ConnectionString))
            {
                // Run a query to engage EF migrations
                (from b in db.Terceros
                 select b).Count();
            }

            // Register the mapping of the tenant to the shard in the shard map.
            // After this step, DDR on the shard map can be used
            PointMapping<int> mapping;
            if (!this.ShardMap.TryGetMappingForKey(key, out mapping))
            {
                this.ShardMap.CreatePointMapping(key, shard);
            }
        }
    }

In the code implementing database creation: DbUtils.CreateDatabaseIfNotExists() -- you are probably using a T-SQL CREATE DATABASE command to Create an Azure database on a logical server. 在实现数据库创建的代码中:DbUtils.CreateDatabaseIfNotExists()-您可能正在使用T-SQL CREATE DATABASE命令在逻辑服务器上创建Azure数据库。 Currently CREATE DATABASE doesn't support specifying the Pool -- however an update to Azure DB is expected within the next month that will extend the functionality of CREATE DATABASE and ALTER DATABASE to specify the Pool name as well. 当前,CREATE DATABASE不支持指定池-但是,预计下个月将对Azure DB进行更新,这将扩展CREATE DATABASE和ALTER DATABASE的功能以指定池名称。

In the meantime, you can make a REST API call from the CreateDatabaseIfNotExists() routine to add the database to the pool once it is created, using the Update SQL Database command: https://msdn.microsoft.com/en-us/library/azure/mt163677.aspx . 同时,您可以使用Update SQL Database命令从CreateDatabaseIfNotExists()例程进行REST API调用,以便在数据库创建后将其添加到池中: https : //msdn.microsoft.com/zh-cn/库/azure/mt163677.aspx

However, making a rest call from inside your c# can be complex, and is discussed here: http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client . 但是,从c#内部进行rest调用可能很复杂,在此处进行了讨论: http : //www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net -客户端 It may be simpler to wait for the upcoming support in the CREATE DATABASE command, which would require a very small modification within your CreateDatabaseIfNotExists() routine. 在CREATE DATABASE命令中等待即将到来的支持可能会更简单,这将需要在CreateDatabaseIfNotExists()例程中进行非常小的修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在弹性池中以编程方式创建Azure SQL数据库? - How to programmatically create an Azure SQL Database in an Elastic Pool? 以编程方式将用户添加到以编程方式创建的数据库 (Azure SQL) - Programmatically add user to programmatically created database (Azure SQL) 具有弹性池的实体框架。 如何管理我的SaaS客户端数据库? - Entity Framework with elastic pool. How to manage my SaaS client database? 以编程方式创建新的IIS网站时,如何将其添加到现有的应用程序池中? - When programmatically creating a new IIS web site, how can I add it to an existing application pool? 如何在IIS 8上以编程方式设置应用程序池标识 - How to programmatically set App Pool Identity on IIS 8 如何以编程方式设置应用程序池空闲超时? - how to set application pool idle timeout programmatically? 如何以编程方式在Basic Edition中创建sql数据库? 在Windows Azure中 - How to create a sql database in Basic edition programmatically? in Windows Azure 如何以编程方式在SQL Server 2005数据库中查找多对多关系 - How to programmatically find a many to many relation in an SQL Server 2005 database C#:如何以编程方式将SQL脚本导入数据库? - C#: How to import SQL-script into database programmatically? 如何在C#中以编程方式创建SQL Server数据库 - How to create a SQL Server database programmatically in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM