如何首先通过Enity Framework代码以编程方式创建基本/标准版类型的Sql Azure数据库

Ogn*_*rov 4 entity-framework ef-code-first ef-migrations entity-framework-6 azure-sql-database

我使用EF 6.我现有的代码是:

public void CreateOrUpdateCompanyDb(string companyDbName)
{
        try
        {
            string connectionString = _connectionStringProvider.GetConnectionString(companyDbName);
            DbMigrationsConfiguration cfg = CreateMigrationsConfig(connectionString);
            cfg.AutomaticMigrationsEnabled = false;
            cfg.AutomaticMigrationDataLossAllowed = false;
            DbMigrator dbMigrator = new DbMigrator(cfg);

            dbMigrator.Update();
        }
        catch (MigrationsException exception)
        {
            _logger.Error(string.Format("Error creating company database '{0}'",companyDbName), exception);
        }
}
Run Code Online (Sandbox Code Playgroud)

连接字符串如下:

Server=tcp:xxx.database.windows.net,1433;Database=companyDbName;User ID=xxx@xxx;Password=xxx;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"
Run Code Online (Sandbox Code Playgroud)

这为特定公司创建了数据库.但问题是创建的数据库来自现已退役的网络版,但我想创建基本/标准/高级版.

我应该如何操作连接字符串,以便数据库的版本是所需的?

Vik*_*pta 8

您可以在Create Database命令中指定Sql Azure数据库版本.AFAIK,您无法使用连接字符串指定它.

句法 -

CREATE DATABASE database_name [ COLLATE collation_name ]
{
   (<edition_options> [, ...n]) 
}

<edition_options> ::= 
{
      MAXSIZE = { 100 MB | 500 MB | 1 | 5 | 10 | 20 | 30 … 150…500 } GB  
    | EDITION = { 'web' | 'business' | 'basic' | 'standard' | 'premium' } 
    | SERVICE_OBJECTIVE = { 'shared' | 'basic' | 'S0' | 'S1' | 'S2' | 'P1' | 'P2' | 'P3' } 
}
[;]
Run Code Online (Sandbox Code Playgroud)

鉴于此,对于您的场景,我会想到两个选项 -

  1. 在使用之前DbMigrator,显式编写创建数据库的代码(如果使用传统不存在)ADO.Net.

  2. 想到的另一个选项,但我不太了解,你可以深入研究,如果你想,是以某种方式找到一种方法来挂钩到EF,这样你就可以自定义Create Database它必须生成的命令.

  • 对于所有偶然发现的人:另一种方法[这里](https://social.technet.microsoft.com/Forums/office/en-US/7d7b5cd8-5878-4241-a674-33336010f081/set-service-tiers-when- create-azure-sql-database-from-vs-c-entity-framework?forum = ssdsgetstarted)和另一个[这里](http://blog.siliconvalve.com/2014/11/12/use-azure-management -API-SDK功能于一个实体框架-定制数据库初始化/) (3认同)