/// <summary> /// Creates an Azure database. /// </summary> /// <param name="context">The DbContext</param> /// <param name="azureSqlDatabaseServiceObjective"></param> /// <param name="connectionString">Optional connection string to use instead of taking it off of <paramref name="context"/></param> /// <exception cref="System.ArgumentException">Not Azure database based on ConnectionString</exception> /// <remarks> /// See https://msdn.microsoft.com/en-us/library/dn268335.aspx for more info. /// 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' } /// </remarks> internal static void CreateAzureDatabase( this DbContext context, AzureSqlDatabaseServiceObjective azureSqlDatabaseServiceObjective, string connectionString = null) { if (!context.IsAzureDatabase()) { throw new ArgumentException("Not Azure database based on ConnectionString"); } var connstrBldr = new SqlConnectionStringBuilder(connectionString ?? context.Database.Connection.ConnectionString) { InitialCatalog = "master" }; if (azureSqlDatabaseServiceObjective == null) { azureSqlDatabaseServiceObjective = new AzureSqlDatabaseServiceObjective("Standard", "S0", 2 * 1024); } var databaseName = context.Database.Connection.Database; var dbCreationCmd = $"CREATE DATABASE [{databaseName}] (MAXSIZE={azureSqlDatabaseServiceObjective.MaxSizeInMegaBytes}MB," + $"EDITION='{azureSqlDatabaseServiceObjective.Edition}'," + $"SERVICE_OBJECTIVE='{azureSqlDatabaseServiceObjective.ServiceObjective}')"; // With Azure SQL db V12, database creation TSQL became a sync process. // So we need a 10 minutes command timeout ExecuteNonQuery(connstrBldr.ConnectionString, dbCreationCmd, commandTimeout: 600); context.WaitUntilDatabaseIsCreated(false); }
/// <summary> /// Set Azure Databse SKU and Size /// </summary> /// <param name="azureSqlDatabaseServiceObjective"></param> /// <returns></returns> public CreateAndMigrate <TContext> WithAzureSqlDatabaseServiceObjective(AzureSqlDatabaseServiceObjective azureSqlDatabaseServiceObjective) { if (azureSqlDatabaseServiceObjective == null) { throw new ArgumentNullException(nameof(azureSqlDatabaseServiceObjective)); } this.azureSqlDatabaseServiceObjective = azureSqlDatabaseServiceObjective; return(this); }