从VS C#创建Azure SQL数据库时设置服务层

时间:2014-11-11 00:55:45

标签: c# entity-framework visual-studio azure azure-sql-database

在C#中从Visual Studio创建新数据库时是否可以设置Microsoft Azure SQL数据库服务层?目前,我可以连接到Azure SQL服务器并创建表没有问题但由于某种原因(可能是Microsoft Default),数据库将在Web中创建,这是将要停用的服务层。我想根据需要将默认服务层设置为Basic,Standard或Premium。

到目前为止我发现的是当我将此方法称为Database.Initialize(true)< - EF时 http://msdn.microsoft.com/en-us/library/system.data.entity.database.initialize(v=vs.113).aspx 它将创建数据库并将其设置为Web服务层。

2 个答案:

答案 0 :(得分:1)

使用Azure SQL v12,您可以选择指定SKU。 例如:

var dbCreationCmd = $"CREATE DATABASE [{databaseName}] (MAXSIZE={maxSize}," +
                            $"EDITION='{edition}'," +
                            $"SERVICE_OBJECTIVE='{serviceObjective}')";

// With Azure SQL db V12, database creation TSQL became a sync process. 
// So we need a 10 minutes command timeout
ExecuteNonQuery(connectionString, dbCreationCmd, commandTimeout: 600);

答案 1 :(得分:0)

如Simon所述,SQL数据层只能在配置数据库后完成。

从Powershell,这是一个可以称为数据库后期配置的功能

Function Update-DatabaseServiceTier
{
    Param
    (
        [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=0)][ValidateNotNullOrEmpty()]
        [String]$databasename,
        [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=1)][ValidateNotNullOrEmpty()]
        [String]$PerformanceLevel,
        [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=2)][ValidateNotNullOrEmpty()]
        [String]$Edition,
        [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=3)][ValidateNotNullOrEmpty()]
        [String]$MaxSize,
        [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=4)][ValidateNotNullOrEmpty()]
        [String]$SQLServerName,
        [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=5)][ValidateNotNullOrEmpty()]
        [String]$userId,
        [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=6)][ValidateNotNullOrEmpty()]
        [String]$password
    )  
         # Get current Database Details
         $DatabaseDetails = Get-AzureSqlDatabase -ServerName $SQLServerName -DatabaseName $databasename -ErrorAction Stop -WarningAction SilentlyContinue
         $currentEdition = $DatabaseDetails.Edition
         $currentSize = $DatabaseDetails.MaxSizeGB

         if (($currentEdition -ne $Edition) -or ($currentSize -ne $MaxSize))
         {
             Write-Verbose " Upgrading the Database Edition - Database Size"

             # Set SQL Server Connection Context
             $server = Get-AzureSqlDatabaseServer $SQLServerName -ErrorAction Stop -WarningAction SilentlyContinue
             $servercredential = New-object System.Management.Automation.PSCredential($userId, ($password | ConvertTo-SecureString -asPlainText -Force))
             $ctx = $server | New-AzureSqlDatabaseServerContext -Credential $serverCredential

             $db = Get-AzureSqlDatabase $ctx –DatabaseName $databasename -ErrorAction Stop -WarningAction SilentlyContinue
             $PL = Get-AzureSqlDatabaseServiceObjective -Context $ctx -ServiceObjectiveName $PerformanceLevel -ErrorAction Stop -WarningAction SilentlyContinue

             # Update SQL Server Properties (Service Objective, Edition and Size)
             Set-AzureSqlDatabase -ConnectionContext $ctx –Database $db -ServiceObjective $PL -Edition $Edition -MaxSizeGB $MaxSize -Force -ErrorAction Stop -WarningAction SilentlyContinue

         }
         else
         {
            Write-Verbose ""
            Write-Verbose " Database Edition and Size upto date!!"
         }
}