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

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

问题描述

我使用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异常)
{
_logger.Error(string.Format(创建公司数据库{0},companyDbName时出错),异常);
}
}

连接字符串如下:

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

为特定公司创建数据库。但是问题是创建的数据库是从现在退出的Web版本,但是我想创建基本/标准/高级版。



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

解决方案

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



语法 -

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

< edition_options> :: =
{
MAXSIZE = {100 MB | 500 MB | 1 | 5 | 10 | 20 | 30 ... 150 ... 500} GB
| EDITION = {'web'| 商业| '基本'| '标准'| 'premium'}
| SERVICE_OBJECTIVE = {'shared'| '基本'| 'S0'| 'S1'| 'S2'| 'P1'| 'P2'| 'P3'}
}
[;]

在使用 DbMigrator 之前,请注意两个选项 -


  1. 明确地写代码创建数据库,如果它不存在使用传统的 ADO.Net


  2. 另一个选项是想到,但我不够了解,你可以挖掘,如果你想,是以某种方式找到一种方法来钩入EF,以便您可以自定义创建数据库命令必须生成。



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

with connection string as follows :

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?

解决方案

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.

Syntax -

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. Before using DbMigrator, explicitly write code which creates the database, if it does not exist using traditional ADO.Net.

  2. 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天全站免登陆