I have two objects with one-to-many relationship with following mapping definition. Whenever, I delete a CostCentre, all LeasePty that reference it, will have the CostCentre_id set to null and the CostCentre will be deleted. Whereas, I wish the system give me an exception to tell me that the CostCentre is being referenced and cannot be deleted when I try to delete the CostCentre.
I tried to change the reference to mapping.References(x => x.CostCentre).WithForeignKey().not.nullable. However, this give me an database error when the transaction is actually committed. Can I get an exception at an earlier stage? Thanks!
public class CostCentreMap : IAutoMappingOverride<CostCentre> { public void Override(AutoMap<CostCentre> mapping) {
mapping.Id(x => x.Id) .WithUnsavedValue(0) .GeneratedBy.Native();
mapping.HasMany(x => x.LeasePties) .AsBag();
} } public class LeasePtyMap : IAutoMappingOverride<LeasePty> { public void Override(AutoMap<LeasePty> mapping) {
mapping.Id(x => x.Id, "ID") .WithUnsavedValue(0) .GeneratedBy.Native();
mapping.Map(x => x.EngAddr1) .Not.Nullable() .WithLengthOf(30);
mapping.Map(x => x.EngAddr2) .Nullable() .WithLengthOf(30);
mapping.OptimisticLock.Version(); mapping.DynamicUpdate(); mapping.Version(x => x.Version);
/*-------------------------------------- Key --------------------------------------*/ mapping.References(x => x.CostCentre) .WithForeignKey(); }
}
|