views:

25

answers:

1

I can't get NHibernate to delete this child object, it completes without throwing any exceptions and without deleting anything:

    public void DeleteW9(int vendorId, int vendorW9Id)
    {
        var vendor = vendorRepository.Get(vendorId);
        var W9 = vendor.W9.Where(x => x.Id == vendorW9Id).First();
        vendor.W9.Remove(W9);
        vendorRepository.SaveOrUpdate(vendor);
    }

Here's my Vendor mapping:

        mapping.HasMany(x => x.W9)
               .KeyColumn("VendorFk")
               .Cascade.AllDeleteOrphan()
               .AsBag();

My VendorW9 table contains a reference to the Vendor's ID in the form of the VendorFk. I have no restraints setup, do I need to setup a primary key relationship? NHibernate functions fine for everything but deleting orphans.

+1  A: 

Try adding vendorRepository.Commit():

public void DeleteW9(int vendorId, int vendorW9Id) 
{ 
    var vendor = vendorRepository.Get(vendorId); 
    var W9 = vendor.W9.Where(x => x.Id == vendorW9Id).First(); 
    vendor.W9.Remove(W9); 
    vendorRepository.SaveOrUpdate(vendor);
    vendorRepository.Commit();
} 
Michael Goldshteyn
That's it, thanks!
chum of chance
Can you explain why this works?
UpTheCreek
It seems NHibernate needs to be explicitly told to commit the changes in order to remove orphans.
chum of chance