Fluent NHibernate - Session.Merge causes NHibernate.LazyInitializationException : illegal access to loading collection

Showing 1-1 of 1 messages
Fluent NHibernate - Session.Merge causes NHibernate.LazyInitializationException : illegal access to loading collection Darko@Krim 10/24/12 4:04 AM

I get this error when trying to save an item with Merge. The item has a bag (Children) which I like to be bindable, hence transformed to an ObservableCollection with a custom accessor.

This is the mapping:
RootItemMap() {
            Id(x => x.Id);
            Map(x => x.Description);
            HasMany<ChildItem>(x => x.Children).Access.Using<ObservableAccessor<ChildItem>>().KeyColumn("RootId").Cascade.All();
}

This works fine in on the client. I can add and remove items and the UI responds fine. But when I try to save the changes with session.Merge I get the above exception.

Any ideas? If I add a Try-Catch in the Set method this works just fine.

Here is a test class that illustrates the fault (ThenItCanBeSavedWithMerge causes the error):
namespace MergeProblem.Tests
{
    [TestFixture]
    public class WhenWorkingWithRootItemWithChildren
    {
        private int _id;
        private RootItem _root;

        [TestFixtureSetUp]
        public void AddRootWithChildrenToDatabase()
        {
            var root = new RootItem();
            root.Description = "My decription";
            IList<ChildItem> children = new ObservableCollection<ChildItem>() { new ChildItem() { Description = "First child" }, new ChildItem() { Description = "Second child"}};
            root.Children = children;

            using (var session = SessionManager.GetSession())
            {
                using (var tx = session.BeginTransaction())
                {
                    session.SaveOrUpdate(root);
                    tx.Commit();
                }

                _id = root.Id;
                _root = root;
            }
        }

        [Test]
        public void ThenItCanBeAccessed()
        {
            using (var session = SessionManager.GetSession())
            {
                var root = session.Get<RootItem>(_id);

                Assert.IsNotNull(root, "Root is null");
                Assert.AreEqual(_id, root.Id, "Id incorrect");
                Assert.IsNotNull(root.Children, "Children is null");
                Assert.AreEqual(2, root.Children.Count);
            }
        }

        [Test]
        public void ThenTheChildrenCollectionIsObservable()
        {         
            Assert.That(_root.Children, Is.InstanceOf(typeof(INotifyCollectionChanged)), "We want the collection to be observable");
        }

        [Test]
        public void ThenItCanBeSavedWithMerge()
        {
            var newChild = new ChildItem() { Description = "New child" };
            _root.Children.Add(newChild);

            using (var session = SessionManager.GetSession())
            {
                using (var tx = session.BeginTransaction())
                {
                    _root = session.Merge(_root);
                    tx.Commit();
                }
            }

            Assert.AreEqual(3, _root.Children.Count);
        }
    }
}