I have a Car entity which possesses doors. When the car is deleted, the doors should be deleted too because they don't make sense on their own. Here's the mapping in FluentNHibernate.
public class CarMap : ClassMap<Car>
{
public CarMap()
{
Id(x => x.CarId).GeneratedBy.Assigned();
HasMany(x => x.Doors).Cascade.AllDeleteOrphan();
}
}
public class DoorMap : ClassMap<Door>
{
public DoorMap()
{
Id(x => x.DoorId);
References(x => x.Car);
}
}
The Delete method inside the CarDao looks like :
public void Delete(Car car)
{
using (ISession session = NHibernateHelper.OpenSession())
using (ITransaction transaction = session.BeginTransaction())
{
session.Delete(car);
transaction.Commit();
}
}
However, when deleting a car this way, the doors are not deleted, but the carId is put de NULL. I thought that the Cascade.AllDeleteOrphan(), would take care of deleting the children (Doors). I had to rework the Delete method to this:
public void Delete(Car car)
{
using (ISession session = NHibernateHelper.OpenSession())
using (ITransaction transaction = session.BeginTransaction())
{
var entity = session.Get<Car>(car.CarId);
if (entity != null)
{
session.Delete(entity);
transaction.Commit();
}
}
}
I feel like there is something I'm missing here, because it doesn't feel right to have to get an object before deleting it. Any thoughts?