/// <summary>
        /// Refresh the object by requerying for the object and merge changes.
        /// </summary>
        /// <param name="database">The object to refresh.</param>
        protected Database RefreshEntity(Database database)
        {
            MergeOption tempOption = this.MergeOption;
            this.MergeOption = MergeOption.OverwriteChanges;
            this.Databases.Where(s => s.Id == database.Id).SingleOrDefault();
            this.MergeOption = tempOption;

            return database;
        }
        /// <summary>
        /// Revert the changes made to the given object, detach it from the context.
        /// </summary>
        /// <param name="database">The object that is being operated on.</param>
        protected void RevertChanges(Database database)
        {
            // Revert the object by requerying for the object and clean up tracking
            if (database != null)
            {
                this.RefreshEntity(database);
            }

            this.ClearTrackedEntity(database);
        }
        /// <summary>
        /// Creates a new Sql Database.
        /// </summary>
        /// <param name="databaseName">The name for the new database.</param>
        /// <param name="databaseMaxSize">The max size for the database.</param>
        /// <param name="databaseCollation">The collation for the database.</param>
        /// <param name="databaseEdition">The edition for the database.</param>
        /// <returns>The newly created Sql Database.</returns>
        public Database CreateNewDatabase(
            string databaseName,
            int? databaseMaxSize,
            string databaseCollation,
            DatabaseEdition databaseEdition)
        {
            // Create a new request Id for this operation
            this.clientRequestId = SqlDatabaseManagementHelper.GenerateClientTracingId();

            // Create the new entity and set its properties
            Database database = new Database();
            database.Name = databaseName;

            if (databaseMaxSize != null)
            {
                database.MaxSizeGB = (int)databaseMaxSize;
            }

            if (!string.IsNullOrEmpty(databaseCollation))
            {
                database.CollationName = databaseCollation;
            }

            if (databaseEdition != DatabaseEdition.None)
            {
                database.Edition = databaseEdition.ToString();
            }

            // Save changes
            this.AddToDatabases(database);
            try
            {
                this.SaveChanges(SaveChangesOptions.None);

                // Re-Query the database for server side updated information
                database = this.RefreshEntity(database);
                if (database == null)
                {
                    throw new ApplicationException(Resources.ErrorRefreshingDatabase);
                }
            }
            catch
            {
                this.ClearTrackedEntity(database);
                throw;
            }

            // Load the extra properties for this object.
            database.LoadExtraProperties(this);

            return database;
        }
        /// <summary>
        /// Given a <see cref="SqlDatabaseResponse"/> this will create and return a <see cref="Database"/> 
        /// object with the fields filled in.
        /// </summary>
        /// <param name="response">The response to turn into a <see cref="Database"/></param>
        /// <returns>a <see cref="Database"/> object.</returns>
        private Database CreateDatabaseFromResponse(SqlDatabaseResponse response)
        {
            Database result = new Database()
            {
                CollationName = response.CollationName,
                CreationDate = DateTime.Parse(response.CreationDate, CultureInfo.InvariantCulture),
                Edition = response.Edition,
                Id = int.Parse(response.Id),
                IsFederationRoot = bool.Parse(response.IsFederationRoot),
                IsSystemObject = bool.Parse(response.IsSystemObject),
                MaxSizeGB = int.Parse(response.MaxSizeGB),
                MaxSizeBytes = long.Parse(response.MaxSizeBytes),
                Name = response.Name,
            };

            // Parse the service objective information
            if (!string.IsNullOrEmpty(response.ServiceObjectiveAssignmentErrorCode))
            {
                result.ServiceObjectiveAssignmentErrorCode = int.Parse(response.ServiceObjectiveAssignmentErrorCode);
            }
            if (!string.IsNullOrEmpty(response.ServiceObjectiveAssignmentErrorDescription))
            {
                result.ServiceObjectiveAssignmentErrorDescription = response.ServiceObjectiveAssignmentErrorDescription;
            }
            if (!string.IsNullOrEmpty(response.ServiceObjectiveAssignmentState))
            {
                result.ServiceObjectiveAssignmentState = byte.Parse(response.ServiceObjectiveAssignmentState);
            }
            if (!string.IsNullOrEmpty(response.ServiceObjectiveAssignmentStateDescription))
            {
                result.ServiceObjectiveAssignmentStateDescription = response.ServiceObjectiveAssignmentStateDescription;
            }
            if (!string.IsNullOrEmpty(response.ServiceObjectiveAssignmentSuccessDate))
            {
                result.ServiceObjectiveAssignmentSuccessDate = DateTime.Parse(response.ServiceObjectiveAssignmentSuccessDate, CultureInfo.InvariantCulture);
            }
            if (!string.IsNullOrEmpty(response.ServiceObjectiveId))
            {
                result.ServiceObjectiveId = Guid.Parse(response.ServiceObjectiveId);
            }

            result.LoadExtraProperties(this);

            return result;
        }