This is driving me crazy. I can get one of the collections to save on a class, but not the other.
I have a class called Category
public Category()
{
Items = new List<Item>();
Prices = new List<Price>();
}
That has these two methods that are pretty much identical. The constructors set a property called Category
and their Name
and Price
respectively.
public virtual Item AddItem(string name)
{
var item = new Item(this, name);
Items.Add(item);
return item;
}
public virtual Price AddPrice(decimal price)
{
var price = new Price(this, price);
Prices.Add(price);
return price;
}
Mappings
public class CategoryMap : ClassMap<Category>
{
public CategoryMap()
{
// Other properties
HasMany(x=>x.Items).Cascade.AllDeleteOrphan();
HasMany(x=>x.Prices).Cascade.AllDeleteOrphan();
}
}
As you can see, the two collections are mapped the exact same way.
The maps for Price
and Item
both have the same mapping
References(x=>x.Category);
As far as I can tell, almost everything about these two are identical. Here's the problem
category.AddItem(someName);
session.Save(category); // Works
category.AddPrice(somePrice);
session.Save(category); // Doesn't work
These are in a file I use to populate a test database. Saving the Item works fine. Saving the Price does not. The classes and the mappings are identical. I can't figure it out.
The mappings should work fine as calling the constructor directly works fine
session.Save(new Price(category, 1));
So why does Category save one collection and not the other? I ran profiler, and the class isn't even trying to save the Price collection.
Update:
As Gabe points out in the comment, if I swap the calls so that Price is called before Item, the Price goes into the database and Item does not.
If I call session.Flush()
after every collection update, it works fine. Am I supposed to have to do this, or is there a way to fix my mapping so that it works?