/// <summary>
 /// Updates the properties of an Azure SQL Database.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Sql.IDatabaseOperations.
 /// </param>
 /// <param name='serverName'>
 /// Required. The name of the Azure SQL Database Server where the
 /// database is hosted.
 /// </param>
 /// <param name='databaseName'>
 /// Required. The name of the Azure SQL Database to be updated.
 /// </param>
 /// <param name='parameters'>
 /// Required. The parameters for the Update Database operation.
 /// </param>
 /// <returns>
 /// Contains the response from a request to Update Database.
 /// </returns>
 public static Task<DatabaseUpdateResponse> UpdateAsync(this IDatabaseOperations operations, string serverName, string databaseName, DatabaseUpdateParameters parameters)
 {
     return operations.UpdateAsync(serverName, databaseName, parameters, CancellationToken.None);
 }
 /// <summary>
 /// Updates the properties of an Azure SQL Database.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Sql.IDatabaseOperations.
 /// </param>
 /// <param name='serverName'>
 /// Required. The name of the Azure SQL Database Server where the
 /// database is hosted.
 /// </param>
 /// <param name='databaseName'>
 /// Required. The name of the Azure SQL Database to be updated.
 /// </param>
 /// <param name='parameters'>
 /// Required. The parameters for the Update Database operation.
 /// </param>
 /// <returns>
 /// Contains the response from a request to Update Database.
 /// </returns>
 public static DatabaseUpdateResponse Update(this IDatabaseOperations operations, string serverName, string databaseName, DatabaseUpdateParameters parameters)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IDatabaseOperations)s).UpdateAsync(serverName, databaseName, parameters);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
        /// <summary>
        /// Update a database on the server.
        /// </summary>
        /// <param name="databaseName">The name of the database to modify.</param>
        /// <param name="newDatabaseName">The new name of the database.</param>
        /// <param name="databaseMaxSizeInGB">The new maximum size of the database.</param>
        /// <param name="databaseEdition">The new edition of the database.</param>
        /// <param name="serviceObjective">The new service objective of the database.</param>
        /// <returns>The updated database.</returns>
        public Database UpdateDatabase(
            string databaseName,
            string newDatabaseName,
            int? databaseMaxSizeInGB,
            long? databaseMaxSizeInBytes,
            DatabaseEdition? databaseEdition,
            ServiceObjective serviceObjective)
        {
            this.clientRequestId = SqlDatabaseCmdletBase.GenerateClientTracingId();

            // Get the SQL management client
            SqlManagementClient sqlManagementClient = AzureSession.ClientFactory.CreateClient<SqlManagementClient>(this.profile, subscription, AzureEnvironment.Endpoint.ServiceManagement);
            this.AddTracingHeaders(sqlManagementClient);

            // Retrieve the specified database
            DatabaseGetResponse database = sqlManagementClient.Databases.Get(
                this.serverName,
                databaseName);

            DatabaseUpdateParameters parameters = new DatabaseUpdateParameters()
            {
                Name = !string.IsNullOrEmpty(newDatabaseName) ? newDatabaseName : database.Database.Name,
                MaximumDatabaseSizeInGB = databaseMaxSizeInGB,
                MaximumDatabaseSizeInBytes = databaseMaxSizeInBytes,
            };
            parameters.Edition = (database.Database.Edition ?? string.Empty);
            if (databaseEdition.HasValue)
            {
                if (databaseEdition != DatabaseEdition.None)
                {
                    parameters.Edition = databaseEdition.ToString();
                }
            }
            parameters.ServiceObjectiveId = database.Database.ServiceObjectiveId;
            if (serviceObjective != null)
            {
                parameters.ServiceObjectiveId = serviceObjective.Id.ToString();
            }

            // Update the database with the new properties
            DatabaseUpdateResponse response = sqlManagementClient.Databases.Update(
                this.serverName,
                databaseName,
                parameters
                );

            // Construct the resulting Database object
            Database updatedDatabase = CreateDatabaseFromResponse(response);
            return updatedDatabase;
        }
 /// <summary>
 /// The Get Database operation retrieves information about a SQL Server
 /// database.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.Sql.IDatabaseOperations.
 /// </param>
 /// <param name='serverName'>
 /// The name of the SQL Server where the database is housed.
 /// </param>
 /// <param name='databaseName'>
 /// The name of the SQL Server database to be obtained.
 /// </param>
 /// <param name='parameters'>
 /// The parameters for the update database operation.
 /// </param>
 /// <returns>
 /// A standard service response including an HTTP status code and
 /// request ID.
 /// </returns>
 public static DatabaseUpdateResponse Update(this IDatabaseOperations operations, string serverName, string databaseName, DatabaseUpdateParameters parameters)
 {
     try
     {
         return operations.UpdateAsync(serverName, databaseName, parameters).Result;
     }
     catch (AggregateException ex)
     {
         if (ex.InnerExceptions.Count > 1)
         {
             throw;
         }
         else
         {
             throw ex.InnerException;
         }
     }
 }
        internal async Task<DatabaseUpdateResponse> UpdateDatabaseAsync(string serverName, string databaseName, string newName, string edition, int? maxSizeInGB)
        {
            var p = new DatabaseUpdateParameters();
            if (!string.IsNullOrEmpty(newName)) { p.Name = newName; }
            if (!string.IsNullOrEmpty(edition)) { p.Edition = newName; }
            if (maxSizeInGB != null) { p.MaximumDatabaseSizeInGB = maxSizeInGB; }

            return await _sqlManagementClient.Databases.UpdateAsync(serverName, databaseName, p);
        }