Hi,

I can't figure out how to get rid of the following behavior:

I have an entity, say Parent, which has an signle assigned identifier
(so that there'll always be one single record in the corresponding
database table) and a collection (IList<>) of Child entities. Parent
is mapped with "HasMany(x =>
x.Children).Access.ReadOnlyPropertyThroughLowerCaseField().Cascade.AllDeleteOrphan()".
The Child entity has no reference to the Parent so it's mapped
regularly.

Then, in my repository, because of the assigned identifier I can't
call SaveOrUpdate, so I do this:

        public Parent Save(Parent parent)
       {
            if (Get() != null)
            {
                return repository.Update(parent);
            }
            else
            {
                return repository.Save(parent);
            }
         }

If I create two instances of Parent and call Save successively like
so...

        // Note: parents have the Children collections populated
        var p1 = new Parent();
        var p2 = new Parent();
        repository.Save(p1);
        repository.Save(p2);

...then I get both correct data in the database AND orphaned children
(i.e. with ParentID FK == NULL). The children from the first Save are
orphaned, while the ones from the second one are inserted and then
have their FKs updated to point to the single Parent record.

Any idea what I'm doing wrong?

Thanks

ps: I also tried setting .Inverse() on the Parent HasMany mapping, but
that didn't work either.

-- 
You received this message because you are subscribed to the Google Groups 
"Fluent NHibernate" group.
To post to this group, send email to fluent-nhibern...@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