This is a migrated thread and some comments may be shown as answers.

Cascading delete with IsDependent not working?

1 Answer 35 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Emily
Top achievements
Rank 1
Emily asked on 30 Nov 2015, 06:01 PM

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?

1 Answer, 1 is accepted

Sort by
0
Boris Georgiev
Telerik team
answered on 03 Dec 2015, 03:57 PM
Hi Emily,

The IsDependent flag is applied to the Navigation Property, not to the entire association. When a Navigation Property is marked with IsDependent and you are trying to delete the object with this navigation property, the OpenAccessContext will try to delete the object referred from the Navigation Property.

In your case, because the association is defined from the "VMDrive" configuration and the "HostMachine" Navigation Property is marked with IsDependent, when you try to delete a Drive, the OpenAccessContext will try to delete the "VirtualMachine" referred in the Navigation Property, and this is the reason the Reference Constraint error is thrown.

To fix the issue, you should mark the "Drives" Navigation Property in the "VirtualMachine" class with the IsDependent flag.

I hope that helps.

Regards,
Boris Georgiev
Telerik
 
Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
Tags
Development (API, general questions)
Asked by
Emily
Top achievements
Rank 1
Answers by
Boris Georgiev
Telerik team
Share this question
or