C# 从VS C创建Azure SQL数据库时设置服务层#
在C#中从Visual Studio创建新数据库时,是否可以设置Microsoft Azure SQL数据库服务层?目前,我可以连接到Azure SQL server并创建表,但由于某种原因(可能是Microsoft默认),数据库将在Web中创建,而Web是即将退役的服务层。我想根据需要将默认服务层设置为基本、标准或高级C# 从VS C创建Azure SQL数据库时设置服务层#,c#,entity-framework,visual-studio,azure,azure-sql-database,C#,Entity Framework,Visual Studio,Azure,Azure Sql Database,在C#中从Visual Studio创建新数据库时,是否可以设置Microsoft Azure SQL数据库服务层?目前,我可以连接到Azure SQL server并创建表,但由于某种原因(可能是Microsoft默认),数据库将在Web中创建,而Web是即将退役的服务层。我想根据需要将默认服务层设置为基本、标准或高级 到目前为止,我发现当我调用这个方法Database.Initialize(true)时,正如Simon提到的,SQL数据层只能在配置DB之后才能完成 在Powershell中,
到目前为止,我发现当我调用这个方法Database.Initialize(true)时,正如Simon提到的,SQL数据层只能在配置DB之后才能完成 在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!!"
}
}
使用Azure SQL v12,您可以选择指定SKU。 例如:
设置Azure SQL数据库后,您可以随时更改其运行的层。这也包括将其转移到新的层。谢谢Simon,但是如果我每天创建100个数据库,那么我每天需要创建100个?我希望有更好的方法。那么,应用程序配置文件中的EF连接字符串中出现了什么?这是我在连接字符串服务器中的内容=[removed];综合安全=假;用户ID=[已删除];密码=[已删除];初始目录=[removed]“我为您的情况发布了一个可能的解决方案,但这将意味着要以这种方式进行大量的工作。
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);