views:

32

answers:

1

I have a one-to-many relationship in data model from A to B. But in my domain API I do not expose "B"s on A (since we will never navigate from from A to B), but have a reference from B to A. Now I want to be able to delete all "B"s when A is deleted. Is it possible? Right now NH is trying first to set FK to null, which I don't want, and cannot since column is not nullable.

A = SupplierType

B = BaseProductCoInsurance

   public BaseProductCoInsuranceMap()
        {
            Table("BaseProductCoInsurance");

            Id(x => x.Id, "BaseProductCoInsuranceId");

            Map(x => x.CoInsurancePercent).Column("CoInsrPrcnt");

            References(x => x.BaseProduct, "BaseProductId");
            References(x => x.PolicySupplierType, "PlcySupplierTypeID");
            References(x => x.InsuredType, "InsuredTypeCode");
        }
A: 

If you need to be able to cascade delete then you need to let NHibernate know about the relationship. That said you don't need to make the relationship accessible to others. You could of course have a private collection that only NH knows about.

If you make the relationship lazy loaded you shouldn't even see a performance hit from this.

Another option to consider is to just modify your delete method to also delete the other entity.

ShaneC