I am trying to configure my model to support cascading deletions when a parent row is deleted. A quick rundown of what my model looks like is as follows:
- Class RTVirtualMachine
- Class RTVMDrive
As you can probably guess, the RTVirtualMachine class has the property IList<RTVMDrive> so I can keep track of a VirtualMachines drives that it has attached currently. I read through the Getting Started guide and followed the tutorial on how to set up dependencies so that when a parent row is deleted, all dependent rows are deleted as well. My mapping configuration for this looks like this:
public
void
PrepareRTVMDriveAssociationConfigurations(MappingConfiguration<RTVMDrive> configuration)
{
//Create one-to-many relationshop with RTVirtualMachine
//ONE virtual machine can have MANY VM Drives
configuration.HasAssociation(d => d.HostMachine)
.WithOpposite(v => v.Drives)
.HasConstraint((d, v) => d.HostVmId == v.ID)
.IsDependent().WithDataAccessKind(DataAccessKind.ReadWrite);
}
Up front this works great. If I add an RTVirtualMachine to the context while it has a populated IList of RTVMDrive objects, they all get added to the database and the RTVMDrive's HostVmId matches the associated RTVirtualMachines ID property.
The issue is when I try to delete anything from the RTVirtualMachine table. More specifically for debug purposes I am trying to delete ALL records from the RTVirtualMachine table as follows:
var vmList = dbContext.VirtualMachines;
int
deleted = vmList.DeleteAll();
Console.WriteLine(
"Deleted: {0} vms"
, deleted);
This throws an InvalidOperationException:
System.InvalidOperationException: An exception occurred during the execution of
'Extent<
FluentData.Type.RTVirtualMachine
>()'. Failure: Telerik.Op
enAccess.RT.sql.SQLException: The DELETE statement conflicted with the REFERENCE
constraint "ref_VmDrives_VirtualMachines". The conflict occurred in database "C
loudStats2", table "dbo.VmDrives", column 'HostVmId'.
The statement has been terminated. ---> System.Data.SqlClient.SqlException: The
DELETE statement conflicted with the REFERENCE constraint "ref_VmDrives_VirtualM
achines". The conflict occurred in database "CloudStats2", table "dbo.VmDrives",
column 'HostVmId'.
The statement has been terminated.
I was under the impression that as long as I marked the RTVMDrive mapping as dependent that it would automatically cascade the deletes and not throw an exception like this. Am I doing something wrong?