views:

12

answers:

0

I'm having a lot of trouble with cascading delete all orphans through a HasAndBelongsToMany relationship using Castle ActiveRecord/NHibernate.

I have Facilities which can have different types of Addresses. They are joined by a table called FacilityAddress which just has a FacilityID and an AddressID.

Here is what I want the code to do: If I have two facilities lets say "Fac1" and "Fac2" that are associated to "Address1". When I delete "Fac1", "Fac1" and its association to "Address1" should be deleted leaving "Fac2" and "Address1" still associated.

BUT, when I delete "Fac1" the code currently deletes "Fac1", the association to "Address1", "Address1" plus the association to "Fac2". So the only object left in the database is "Fac2".

I am running this against a SQLite database, which I haven't ruled out as part of the problem.

Here is the code snippet:

[ActiveRecord( "Facility" )]
public class Facility : ActiveRecordBase
{
    [PrimaryKey]
    public int Id { get; private set; }

    [HasAndBelongsToMany(typeof(Address) 
        ,Table = "FacilityAddress" 
        ,ColumnKey = "FacilityID" 
        ,ColumnRef = "AddressID"
        ,Lazy = true
        ,Cascade = ManyRelationCascadeEnum.AllDeleteOrphan
        )]
    public IList<Address> Addresses { get; set; }
}

[ActiveRecord( "Address" )]
public class Address : ActiveRecordBase
{
    [PrimaryKey( "AddressID" )]
    public int AddressID { get; private set; }

    [HasAndBelongsToMany( typeof( Facility )
        ,Table = "FacilityAddress"
        ,ColumnKey = "AddressID"
        ,ColumnRef = "FacilityID"
        ,Lazy = true
        )]
    public IList<Facility> Facilities { get; set; }
}

related questions