I'm a newbie in the NHibernate world.
Why this code works removing the territory from the collection:
Country country;
using (IUnitOfWork unit = UnitOfWork.Start())
{
country = new Country();
country.Name = "My country";
Territory territory = new Territory();
country.Territories.Add(territory);
country.Territories.Remove(territory);
}
And this code is not working:
Country country;
using (IUnitOfWork unit = UnitOfWork.Start())
{
country = _countries.GetById(1);
Territory territory = new Territory();
country.Territories.Add(territory);
country.Territories.Remove(territory);
}
In the second code snippet, _countries is a repository. The country ID 1 exists in the database. The territory is added, but never removed...
Here's the mapping:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="iCible.Artishows.Framework.ObjectDefinition"
namespace="iCible.Artishows.Framework.ObjectDefinition" >
<class name="Country" >
<id name="ID">
<generator class="identity"/>
</id>
<property name="Name" />
<set name="Territories" cascade="all-delete-orphan" inverse="true" order-by="Name" sort="iCible.Artishows.Framework.ObjectDefinition.TerritoryComparer">
<key column="COUNTRYID"/>
<one-to-many class="Territory"/>
</set>
<property name="CreationDate" />
<property name="EditionDate" />
<many-to-one class="User" name="CreationUser"/>
<many-to-one class="User" name="EditionUser"/>
</class>
</hibernate-mapping>
What am I missing here?