如何首先通过 Enity Framework 代码以编程方式创建基本/标准版类型的 Sql Azure 数据库 [英] How to programatically create Sql Azure database of type Basic/Standard edition through Enity Framework code first

本文介绍了如何首先通过 Enity Framework 代码以编程方式创建基本/标准版类型的 Sql Azure 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use EF 6. My existing code is :

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);
        }
}

连接字符串如下:

Server=tcp:xxx.database.windows.net,1433;Database=companyDbName;User ID=xxx@xxx;Password=xxx;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"

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

which creates the database for the particular company. But the problem is that the created database is from the now retired Web Edition but I want to create Basic/Standard/Premium edition.

我应该如何操作连接字符串以使数据库的版本成为所需的版本?

How should I manipulate the connection string so that the edition of the database is the desired one?

推荐答案

Sql Azure Database 的版本可以在 创建数据库命令.AFAIK,您不能使用连接字符串指定它.

The Edition of Sql Azure Database is something you can specify in Create Database command. AFAIK, you can't specify it using the connection string.

语法-

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' } 
}
[;]

鉴于您的场景,我想到了两个选项 -

Given that, for your scenario, two options come to mind -

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

  1. Before using DbMigrator, explicitly write code which creates the database, if it does not exist using traditional ADO.Net.

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

The other option which comes to mind, but I don't know enough about and you could dig into if you want to, is to somehow find a way to hook into EF, so that you could customize the Create Database command it must generate.

这篇关于如何首先通过 Enity Framework 代码以编程方式创建基本/标准版类型的 Sql Azure 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆