Cascading delete with IsDependent not working?

2 posts, 0 answers
  1. Emily
    Emily avatar
    5 posts
    Member since:
    Nov 2015

    Posted 30 Nov 2015 Link to this post

    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?

  2. Boris Georgiev
    Admin
    Boris Georgiev avatar
    190 posts

    Posted 03 Dec 2015 Link to this post

    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.
  3. DevCraft banner
Back to Top