private void WritePreHistory(Guid billDemandUid, Budget2DataContext context, WorkflowState initialState,
                                     WorkflowState destinationState, Guid? expectedInitiatorId, WorkflowCommand command, WorkflowState startState)
        {
            if (initialState.Order < startState.Order)
                return;

            var billDemndHistoryItem = new BillDemandTransitionHistory
                                           {
                                               Id = Guid.NewGuid(),
                                               BillDemandId = billDemandUid,
                                               DestinationStateId = destinationState.DbStateId.Value,
                                               InitialStateId = initialState.DbStateId.Value,
                                               TransitionExpectedInitiatorId = expectedInitiatorId,
                                               CommandId = command.Id,
                                               IsPlanTransition = true,
                                               Description = string.Empty
                                           };
            context.BillDemandTransitionHistories.InsertOnSubmit(billDemndHistoryItem);
        }
        public void UpdateBillDemandState(UpdateBillDemandStateParams updateBillDemandStateParams)
        {
            if (!updateBillDemandStateParams.InitialState.DbStateId.HasValue)
                throw new ArgumentException(
                    "Не определено соттветствие состояния Workflow отображаемому состоянию BillDemand", "updateBillDemandStateParams.InitialState");
            if (!updateBillDemandStateParams.DestinationState.DbStateId.HasValue)
                throw new ArgumentException(
                    "Не определено соттветствие состояния Workflow отображаемому состоянию BillDemand",
                    "updateBillDemandStateParams.DestinationState");
            using (var scope = ReadUncommittedSupressedScope)
            {
                using (var context = CreateContext())
                {
                    BillDemandTransitionHistory billDemndHistoryItem = null;

                    var billDemndHistoryItems = context.BillDemandTransitionHistories.Where(
                        p =>
                        p.BillDemandId == updateBillDemandStateParams.BillDemandUid && p.InitialStateId == updateBillDemandStateParams.InitialState.DbStateId.Value
                        && (p.CommandId == updateBillDemandStateParams.Command.Id || updateBillDemandStateParams.Command.SkipCheckCommandId) && !p.TransitionInitiatorId.HasValue).ToList();

                    if (billDemndHistoryItems.Count == 1)
                        billDemndHistoryItem = billDemndHistoryItems.First();
                    else if (billDemndHistoryItems.Count > 1)
                    {
                        billDemndHistoryItem = GetBillDemndHistoryItem(billDemndHistoryItems, updateBillDemandStateParams.ImpesonatedIdentityId,
                                                                       updateBillDemandStateParams.InitiatorId);
                    }

                    if (billDemndHistoryItem == null)
                    {
                        billDemndHistoryItem = new BillDemandTransitionHistory
                                                   {
                                                       Id = Guid.NewGuid(),
                                                       BillDemandId = updateBillDemandStateParams.BillDemandUid,
                                                       DestinationStateId = updateBillDemandStateParams.DestinationState.DbStateId.Value,
                                                       InitialStateId = updateBillDemandStateParams.InitialState.DbStateId.Value,
                                                       CommandId =
                                                           (updateBillDemandStateParams.Command.Id == WorkflowCommand.Unknown.Id
                                                                ? (Guid?)null
                                                                : updateBillDemandStateParams.Command.Id)
                                                   };
                        context.BillDemandTransitionHistories.InsertOnSubmit(billDemndHistoryItem);
                    }
                    billDemndHistoryItem.DestinationStateId = updateBillDemandStateParams.DestinationState.DbStateId.Value;
                    billDemndHistoryItem.TransitionInitiatorId = updateBillDemandStateParams.InitiatorId;
                    billDemndHistoryItem.TransitionTime = DateTime.Now;
                    billDemndHistoryItem.Comment = updateBillDemandStateParams.Comment;
                    var info = WorkflowStateService.GetWorkflowStateInfo(updateBillDemandStateParams.DestinationState);
                    billDemndHistoryItem.Description = WorkflowCommand.GetCommandDescription(updateBillDemandStateParams.Command,
                                                                                             info == null
                                                                                                 ? string.Empty
                                                                                                 : info.StateVisibleName);

                    billDemndHistoryItem.SightingTime = updateBillDemandStateParams.SightingTime.HasValue ? updateBillDemandStateParams.SightingTime.Value : billDemndHistoryItem.TransitionTime;

                    context.SubmitChanges();
                }

                scope.Complete();
            }
        }
		private void attach_BillDemandTransitionHistories(BillDemandTransitionHistory entity)
		{
			this.SendPropertyChanging();
			entity.SecurityTrustee = this;
		}
		private void detach_BillDemandTransitionHistories1(BillDemandTransitionHistory entity)
		{
			this.SendPropertyChanging();
			entity.SecurityTrustee1 = null;
		}
		private void detach_BillDemandTransitionHistories(BillDemandTransitionHistory entity)
		{
			this.SendPropertyChanging();
			entity.StructDivision = null;
		}
		private void attach_BillDemandTransitionHistories1(BillDemandTransitionHistory entity)
		{
			this.SendPropertyChanging();
			entity.BillDemandState1 = this;
		}
		private void detach_BillDemandTransitionHistories(BillDemandTransitionHistory entity)
		{
			this.SendPropertyChanging();
			entity.InitialState = null;
		}
 partial void DeleteBillDemandTransitionHistory(BillDemandTransitionHistory instance);
 partial void InsertBillDemandTransitionHistory(BillDemandTransitionHistory instance);