Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# NHibernate,在会话外部更新只读集合(已更改不可变集合实例)_C#_Unit Testing_Session_Nhibernate - Fatal编程技术网

C# NHibernate,在会话外部更新只读集合(已更改不可变集合实例)

C# NHibernate,在会话外部更新只读集合(已更改不可变集合实例),c#,unit-testing,session,nhibernate,C#,Unit Testing,Session,Nhibernate,我试图通过NHibernate测试一个简单的对象编辑,我得到了一些奇怪的结果 我将此类标记对象映射为树: public class Tag { public int Id { get; set; } public int Description { get; set; } private IList<Tag> childTag = new List<Tag>(); public virtual IEnumerable<Tag>

我试图通过
NHibernate
测试一个简单的对象编辑,我得到了一些奇怪的结果

我将此类标记对象映射为树:

public class Tag
{
    public int Id { get; set; }
    public int Description { get; set; }
    private IList<Tag> childTag = new List<Tag>();
    public virtual IEnumerable<Tag> ChildTag
    { get { return childTag.ToArray(); } }

    public virtual void Add(Tag child)
    { childTag .Add(child); }

    public virtual bool Remove(Tag child)
    { return childTag .Remove(child); }
}
做一个简单的测试:

        using (IUnitOfWork uow = new UnitOfWork())
        {
            uow.Start();
            uow.TagRepository.Create(Tag);
            uow.Commit();
            uow.End();

            // Act
            Tag.remove(Tag.Children.First());

            uow.Start();
            uow.TagRepository.Update(Tag);
            uow.Commit();
            uow.End();

            uow.Start();
            TagActual = uow.TagRepository.GetById(Tag.Id);

            //Assert
            TagActual.ShouldBeEquivalentTo(Tag);
            uow.End();
        }
我得到这个错误:

NHibernate.HibernateException : changed an immutable collection instance: [Gedi.Domain.Object.Entity.Tag.Children#21]
虽然我在单个会话中运行测试(操作之间没有关闭会话),但一切都进行得很顺利,没有引发异常(除了
NHibernate
没有像我所报告的那样更新表)

需要一些关于这方面的指导,以及解决这一问题的步骤

谢谢

编辑

这就是工作的方式

        using (IUnitOfWork uow = new UnitOfWork())
        {
            uow.Start();
            uow.TagRepository.Create(Tag);
            uow.Commit();
            Tag.Remove(Tag.Children.First());
            uow.TagRepository.Update(Tag);
            uow.Commit();
            uow.End();

            uow.Start();
            TagActual = uow.TagRepository.GetById(Tag.Id);
            TagActual.ShouldBeEquivalentTo(Tag);
            uow.End();
        }
但我认为,在业务代码中,我可能需要在会话之外工作,所以我希望它在任何情况下都能运行

我编辑了一些方法名称,因为我不在数据访问层之外公开会话和会话概念,因为会话是特定于NHibernate的

编辑2:

这也很好:

using (IUnitOfWork uow = new UnitOfWork())
        {
            uow.Start();
            uow.TagRepository.Create(Tag);
            uow.Commit();
            uow.End();

            // Act
            Tag.Description = "EDITED";

            uow.Start();
            uow.TagRepository.Update(Tag);
            uow.Commit();
            uow.End();

            uow.Start();
            TagActual = uow.TagRepository.GetById(Tag.Id);

            //Assert
            TagActual.ShouldBeEquivalentTo(Tag);
            uow.End();
        }
只有集合在会话外出错

我看到了一些选项

  • 使用单个会话并检查是否有方法执行会话
    flush
    ,以将更改持久化到数据库

  • 使用单个会话但添加事务,然后在
    Act
    之前提交它以保持更改

  • 使用合并将元素附加到不同的会话
    会话。合并(标记)

  • 在您打开的每个新会话上从数据库获取元素,而不是使用相同的实例


谢谢您的回答。在我的Uow中,开始创建会话和事务,提交是显而易见的,结束关闭会话。关于你的想法见我的update@gt.guybrush:实例属于某个会话,因此您需要执行某些操作,因此请在其他会话上使用它。通常,您的会话范围足够长,可以避免处理多个会话问题。如果是web应用程序,会话通常是每个请求的,因此在非常奇怪的情况下,您必须处理跨会话问题。您是否测试了将实例附加到不同会话的
会话。合并(标记)
?我不公开会话,因此无法使用合并。此时,我可以使用我发布的编辑。我只是希望能在一次训练中顺利完成。我觉得奇怪的是,除了收藏品,所有的东西都能用。顺便问一下,当我们在一起的时候,你能帮我解答一下我上面提到的相关问题吗?()
using (IUnitOfWork uow = new UnitOfWork())
        {
            uow.Start();
            uow.TagRepository.Create(Tag);
            uow.Commit();
            uow.End();

            // Act
            Tag.Description = "EDITED";

            uow.Start();
            uow.TagRepository.Update(Tag);
            uow.Commit();
            uow.End();

            uow.Start();
            TagActual = uow.TagRepository.GetById(Tag.Id);

            //Assert
            TagActual.ShouldBeEquivalentTo(Tag);
            uow.End();
        }