protected override void GenerateSqlCommand(SqlCommand sqlCommand)
        {
            base.GenerateSqlCommand(sqlCommand);

            if (base.StoreLock.IsValid)
            {
                throw FxTrace.Exception.AsError(new InstancePersistenceCommandException(SR.MultipleLockOwnersNotSupported));
            }

            bool withIdentity;
            IDictionary <XName, InstanceValue> commandMetadata = GetCommandMetadata(out withIdentity);
            SqlParameterCollection             parameters      = sqlCommand.Parameters;
            double lockTimeout = base.Store.BufferedHostLockRenewalPeriod.TotalSeconds;

            this.lockOwnerId = Guid.NewGuid();
            ExtractWorkflowHostType(commandMetadata);

            InstanceValue instanceValue;

            if (commandMetadata.TryGetValue(PersistenceMetadataNamespace.ActivationType, out instanceValue))
            {
                if (withIdentity)
                {
                    throw FxTrace.Exception.AsError(new InstancePersistenceCommandException(SR.IdentityNotSupportedWithActivation));
                }
                if (!PersistenceMetadataNamespace.ActivationTypes.WAS.Equals(instanceValue.Value))
                {
                    throw FxTrace.Exception.AsError(new InstancePersistenceCommandException(SR.NonWASActivationNotSupported));
                }
                this.fireActivatableInstancesEvent = true;
            }

            ArraySegment <byte>[] properties = SerializationUtilities.SerializePropertyBag(commandMetadata, base.Store.InstanceEncodingOption);

            parameters.Add(new SqlParameter {
                ParameterName = "@lockTimeout", SqlDbType = SqlDbType.Int, Value = lockTimeout
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@lockOwnerId", SqlDbType = SqlDbType.UniqueIdentifier, Value = this.lockOwnerId
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@workflowHostType", SqlDbType = SqlDbType.UniqueIdentifier, Value = (base.Store.WorkflowHostType != Guid.Empty) ? base.Store.WorkflowHostType : (object)DBNull.Value
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@enqueueCommand", SqlDbType = SqlDbType.Bit, Value = base.Store.EnqueueRunCommands
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@deleteInstanceOnCompletion", SqlDbType = SqlDbType.Bit, Value = (base.Store.InstanceCompletionAction == InstanceCompletionAction.DeleteAll)
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@primitiveLockOwnerData", SqlDbType = SqlDbType.VarBinary, Size = properties[0].Count, Value = (object)(properties[0].Array) ?? DBNull.Value
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@complexLockOwnerData", SqlDbType = SqlDbType.VarBinary, Size = properties[1].Count, Value = (object)(properties[1].Array) ?? DBNull.Value
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@writeOnlyPrimitiveLockOwnerData", SqlDbType = SqlDbType.VarBinary, Size = properties[2].Count, Value = (object)(properties[2].Array) ?? DBNull.Value
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@writeOnlyComplexLockOwnerData", SqlDbType = SqlDbType.VarBinary, Size = properties[3].Count, Value = (object)(properties[3].Array) ?? DBNull.Value
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@encodingOption", SqlDbType = SqlDbType.TinyInt, Value = base.Store.InstanceEncodingOption
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@machineName", SqlDbType = SqlDbType.NVarChar, Value = SqlWorkflowInstanceStoreConstants.MachineName
            });

            if (withIdentity)
            {
                Fx.Assert(base.Store.DatabaseVersion >= StoreUtilities.Version45, "Should never get here if the db version isn't 4.5 or higher");

                string identityMetadataXml = SerializationUtilities.GetIdentityMetadataXml(base.InstancePersistenceCommand);
                parameters.Add(new SqlParameter {
                    ParameterName = "@identityMetadata", SqlDbType = SqlDbType.Xml, Value = identityMetadataXml
                });
            }
        }
        protected override void GenerateSqlCommand(SqlCommand command)
        {
            SaveWorkflowCommand saveWorkflowCommand = base.InstancePersistenceCommand as SaveWorkflowCommand;
            StringBuilder       commandTextBuilder  = new StringBuilder(SqlWorkflowInstanceStoreConstants.DefaultStringBuilderCapacity);
            double operationTimeout           = this.TimeoutHelper.RemainingTime().TotalMilliseconds;
            SqlParameterCollection parameters = command.Parameters;
            string suspensionReason;
            string suspensionExceptionName;

            parameters.Add(new SqlParameter {
                ParameterName = "@instanceId", SqlDbType = SqlDbType.UniqueIdentifier, Value = base.InstancePersistenceContext.InstanceView.InstanceId
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@surrogateLockOwnerId", SqlDbType = SqlDbType.BigInt, Value = base.StoreLock.SurrogateLockOwnerId
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@handleInstanceVersion", SqlDbType = SqlDbType.BigInt, Value = base.InstancePersistenceContext.InstanceVersion
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@handleIsBoundToLock", SqlDbType = SqlDbType.Bit, Value = base.InstancePersistenceContext.InstanceView.IsBoundToLock
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@timerDurationMilliseconds", SqlDbType = SqlDbType.BigInt, Value = (object)GetPendingTimerExpiration(saveWorkflowCommand) ?? DBNull.Value
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@unlockInstance", SqlDbType = SqlDbType.Bit, Value = saveWorkflowCommand.UnlockInstance
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@suspensionStateChange", SqlDbType = SqlDbType.TinyInt, Value = GetSuspensionReason(saveWorkflowCommand, out suspensionReason, out suspensionExceptionName)
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@suspensionReason", SqlDbType = SqlDbType.NVarChar, Value = (object)suspensionReason ?? DBNull.Value
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@suspensionExceptionName", SqlDbType = SqlDbType.NVarChar, Size = 450, Value = (object)suspensionExceptionName ?? DBNull.Value
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@isCompleted", SqlDbType = SqlDbType.Bit, Value = saveWorkflowCommand.CompleteInstance
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@isReadyToRun", SqlDbType = SqlDbType.Bit, Value = IsReadyToRun(saveWorkflowCommand)
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@workflowHostType", SqlDbType = SqlDbType.UniqueIdentifier, Value = (object)GetWorkflowHostType(saveWorkflowCommand) ?? DBNull.Value
            });
            parameters.Add(new SqlParameter {
                ParameterName = "@operationTimeout", SqlDbType = SqlDbType.Int, Value = (operationTimeout < Int32.MaxValue) ? Convert.ToInt32(operationTimeout) : Int32.MaxValue
            });

            string parameterNames = null;

            if (base.Store.DatabaseVersion >= StoreUtilities.Version45)
            {
                string identityMetadataXml = SerializationUtilities.GetIdentityMetadataXml(saveWorkflowCommand);
                parameters.Add(new SqlParameter {
                    ParameterName = "@identityMetadata", SqlDbType = SqlDbType.Xml, Value = (object)identityMetadataXml ?? DBNull.Value
                });

                parameterNames = SaveWorkflowAsyncResult.storedProcedureParameters;
            }
            else
            {
                parameterNames = SaveWorkflowAsyncResult.storedProcedureParameters40;
            }

            commandTextBuilder.AppendLine(@"set nocount on
                                            set transaction isolation level read committed		
                                            set xact_abort on
                                            begin transaction");

            ExtractServiceDeploymentInformation(saveWorkflowCommand, commandTextBuilder, parameters);

            commandTextBuilder.AppendLine("declare @result int");
            commandTextBuilder.AppendLine(string.Format(CultureInfo.InvariantCulture, "exec @result = {0}.[SaveInstance] {1} ;",
                                                        SqlWorkflowInstanceStoreConstants.DefaultSchema, parameterNames));
            commandTextBuilder.AppendLine("if (@result = 0)");
            commandTextBuilder.AppendLine("begin");

            SerializeAssociatedData(parameters, saveWorkflowCommand, commandTextBuilder);

            commandTextBuilder.AppendLine("commit transaction");
            commandTextBuilder.AppendLine("end");
            commandTextBuilder.AppendLine("else");
            commandTextBuilder.AppendLine("rollback transaction");

            this.commandText = commandTextBuilder.ToString();
        }