Cascade Delete with 2 parents, one child Entity Framework

Viewed 30

I have a parent class called Product:

public class Product
{
    [Key]
    public int ID { get; set; }    
    public ICollection<ProductValue> ProductValues { get; set; }
}

It has a child class called ProductValue

public class ProductValue
{
    [Key]
    public int ID { get; set; }

    public int ProductID { get; set; }

    public Product Product { get; set; }

    public int LengthID { get; set; }

    public Length Length { get; set; }
}

And I have another parent class called Length with its child also being ProductValue

public class Length
{
    [Key]
    public int ID { get; set; }    
    public ICollection<ProductValue> ProductValues { get; set; }
}

ProductValue objects will always have a Product parent and a Length parent. If a Length or Product object is deleted its child ProductValue objects also need to be deleted. However, because ProductValue has multiple parents I get the exception:

Microsoft.Data.SqlClient.SqlException: 'Introducing FOREIGN KEY constraint 'FK_ProductValues_Products_ProductID' on table 'ProductValues' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint or index. See previous errors.'

Is there a way to get around this? I would like to not have to manually delete ProductValue objects.

0 Answers
Related