Данные NHibernate не сохраняются между базой данных и сеансом - PullRequest
0 голосов
/ 11 апреля 2020

У меня есть этот код, который я унаследовал, это Nhiberate, и он использует Виндзорский замок

он использует шаблон синглтона, поэтому все соединения имеют один и тот же экземпляр, ServiceLocator.GetService

вот мой код, после выполнения этого, когда я извлекаю все списки, они являются старыми значениями, а не новыми добавлениями или удалениями, но все находится в БД так, как мы хотим. Я не могу понять это

public void SaveEditAssociatedPMRAccomplishments(string json )
        {
            GenericSaveMod modifications = new JavaScriptSerializer().Deserialize<GenericSaveMod>(json ?? "") ?? new GenericSaveMod();
            IRelDocPatManager relManager = ServiceLocator.GetService<IManagerFactory>().GetRelDocPatManager();
            if (this.CurrentObject == null)
            {
                this.CurrentObject = ServiceLocator
                    .GetService<IManagerFactory>()
                    .GetDocManager()
                    .GetById(this.PmrId);
            }
            foreach (int patId in modifications.Mods.Additions)
            {

                RelDocPat existing =
                    relManager.GetRelDocPatIds(CurrentObject.Id, patId, false);
                if (existing == null)
                {
                    RelDocPat newReldocpat = new RelDocPat
                    {
                        Doc = CurrentObject
                    };
                    newReldocpat.SetAccomplishmentById(accomplishmentId);

                    newReldocpat.UpdatedTimestamp = DateTime.Now;
                    newReldocpat.LastModifiedBy = this.CurrentObject.LastModifiedBy;
                    //relManager.Session.BeginTransaction();
                    relManager.SaveOrUpdate(newReldocpat);

                    //relManager.Session.CommitTransaction();
                    relManager.Session.Flush();
                    relManager.Refresh(newReldocpat);

                    this.CurrentObject.RelDocPats.Add(newReldocpat);
                }
                else
                {
                    if (existing.ActiveFlag == false)
                    {
                        existing.ActiveFlag = true;
                        existing.UpdatedTimestamp = DateTime.Now;
                        existing.LastModifiedBy = this.CurrentObject.LastModifiedBy;
                        relManager.Session.Flush();
                        relManager.SaveOrUpdate(existing);
                        relManager.Refresh(existing);
                    }
                }
            }
            relManager.Session.Flush();

            foreach (int patId in modifications.Mods.Removals)
            {

                RelDocPat existing = relManager.GetRelDocPatIds(CurrentObject.Id, patId, false);
                if (existing == null)
                {
                    // nothing to do  , nothing to remve
                }
                else
                {
                    if (existing.ActiveFlag == true)
                    {
                        existing.ActiveFlag = false;
                        existing.UpdatedTimestamp = DateTime.Now;
                        existing.LastModifiedBy = this.CurrentObject.LastModifiedBy;
                        relManager.Session.Flush();
                        relManager.SaveOrUpdate(existing);
                        relManager.Refresh(existing);
                    }
                    this.CurrentObject.RelDocPats.Remove(existing);
                }
            }
        }
...