Hi,
I get this error when trying to save an aggregate root item with Merge. The 
item is read with Fluent NHibernate. Since I'd like to bind the data to the 
WPF user interface, I use a custom getter/setter to transform collections 
(IList<>) to ObservableCollections.

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();
}
And this is the Set method of the custom accessor:
 public void Set(object target, object value)
            {
                var collection = new 
ObservableCollection<T>((IList<T>)value);
                _property.SetValue(target, collection, null);
            }

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);
        }
    }
}


 

-- 
You received this message because you are subscribed to the Google Groups 
"Fluent NHibernate" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/fluent-nhibernate/-/cRkafkn4u2MJ.
To post to this group, send email to fluent-nhibernate@googlegroups.com.
To unsubscribe from this group, send email to 
fluent-nhibernate+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/fluent-nhibernate?hl=en.

Reply via email to