public void UpdateDemandState(WorkflowState initialState, WorkflowState destinationState, WorkflowCommand command, Guid demandId,
                                      Guid initiatorId, string comment)
        {
            if (!initialState.DbStateId.HasValue)
            {
                throw new ArgumentException(
                          "Не определено соттветствие состояния Workflow отображаемому состоянию Demand", "initialState");
            }
            if (!destinationState.DbStateId.HasValue)
            {
                throw new ArgumentException(
                          "Не определено соттветствие состояния Workflow отображаемому состоянию Demand",
                          "destinationState");
            }
            using (var scope = ReadUncommittedSupressedScope)
            {
                using (var context = CreateContext())
                {
                    var demandHistoryItem =
                        context.DemandTransitionHistories.Where(
                            p =>
                            p.DemandId == demandId && p.InitialStateId == initialState.DbStateId.Value && p.DestinationStateId == destinationState.DbStateId.Value &&
                            (p.CommandId == command.Id || command.SkipCheckCommandId) && !p.TransitionInitiatorId.HasValue).ToList().FirstOrDefault();

                    if (demandHistoryItem == null)
                    {
                        demandHistoryItem = new DemandTransitionHistory()
                        {
                            Id                 = Guid.NewGuid(),
                            DemandId           = demandId,
                            DestinationStateId = destinationState.DbStateId.Value,
                            InitialStateId     = initialState.DbStateId.Value,
                            CommandId          =
                                (command.Id == WorkflowCommand.Unknown.Id
                                                             ? (Guid?)null
                                                             : command.Id),
                        };
                        context.DemandTransitionHistories.InsertOnSubmit(demandHistoryItem);
                    }
                    demandHistoryItem.TransitionInitiatorId = initiatorId;
                    demandHistoryItem.TransitionTime        = DateTime.Now;
                    demandHistoryItem.Comment = comment;
                    var info = WorkflowStateService.GetWorkflowStateInfo(destinationState);
                    demandHistoryItem.Description = WorkflowCommand.GetCommandDescription(command,
                                                                                          info == null
                                                                                                 ? string.Empty
                                                                                                 : info.StateVisibleName);



                    context.SubmitChanges();
                }

                scope.Complete();
            }
        }
        private void WritePreHistory(Guid demandId, Budget2DataContext context, WorkflowState initialState,
                                     WorkflowState destinationState, Guid?expectedInitiatorId, WorkflowCommand command, WorkflowState startState)
        {
            if (initialState.Order < startState.Order)
            {
                return;
            }
            var billDemndHistoryItem = new DemandTransitionHistory
            {
                Id                            = Guid.NewGuid(),
                DemandId                      = demandId,
                DestinationStateId            = destinationState.DbStateId.Value,
                InitialStateId                = initialState.DbStateId.Value,
                TransitionExpectedInitiatorId = expectedInitiatorId,
                CommandId                     = command.Id,
                Comment                       = string.Empty,
                Description                   = string.Empty
            };

            context.DemandTransitionHistories.InsertOnSubmit(billDemndHistoryItem);
        }