(StackOverflow question in case anyone wants
upvotes<http://stackoverflow.com/questions/21246584/adding-a-new-item-to-a-collection-using-fluent-nhibernate-doesnt-insert-the-new>
)

I have a model called `ShoppingLists` and each `ShoppingLists` has a
collection of `ShoppingListItems` called `Items`.  What I would *like* to
be able to do is add a new item to my list as such:

    dbList.Items.Add(new ShoppingListItems(Guid.NewGuid(), identity.UserId,
source.Raw));

I would expect the `ShoppingListItems` to automatically be *linked* to its
parent `ShoppingLists` class, and for NHibernate to create the appropriate
SQL `INSERT` statement when the transaction is committed.  However, instead
I get the exception:

    NHibernate.StaleStateException was unhandled
      HResult=-2146232832
      Message=Unexpected row count: 0; expected: 1
      Source=NHibernate

What I have to do instead is create the object, save it, then add it to the
collection:

    var newItem = new ShoppingListItems(Guid.NewGuid(), identity.UserId,
source.Raw);
    newItem.ShoppingList = dbList;
    session.Save(newItem);
    dbList.Items.Add(newItem);

I'd like to eliminate the need to do this.  My mappings for `ShoppingLists`
is as such:

    Id(x => x.ShoppingListId);

    Map(x => x.UserId).Not.Nullable();
    Map(x => x.Title).Not.Nullable();

    HasMany(x => x.Items)
       .KeyColumn("ShoppingListId")
       .Cascade.Delete(); // If Shopping List is deleted, delete all the
Items that reference this list

And my mappings for `ShoppingListItems` is:

    Id(x => x.ItemId);

    Map(x => x.Raw).Length(50);
    Map(x => x.Qty);
    Map(x => x.Unit);
    Map(x => x.UserId).Not.Nullable();
    Map(x => x.CrossedOut).Not.Nullable();

    References(x => x.Recipe).Column("RecipeId");
    References(x => x.Ingredient).Column("IngredientId");
    References(x => x.ShoppingList).Column("ShoppingListId");

I've tried playing around with `Cascade.All()` on each, to no avoid.  Any
ideas?

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to