EF Core: Is it possible to cascade remove self-referencing objects from database without in-code recursion

Viewed 167

I have self-referencing Directory object. Each directory have parent and list of childs.

Modelbuilder configuration:

 modelBuilder.Entity<Directory>()
            .HasMany(d => d.Directories).WithOne(p => p.Parent).OnDelete(DeleteBehavior.Cascade);

Gives me a following exception on migration:

Microsoft.Data.SqlClient.SqlException: 'Introducing FOREIGN KEY constraint 'FK_DirectoryReference_DirectoryReference_ParentId' on table 'DirectoryReference' 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.'

I know how to delete my directories by recursion, but if it possible I would like SQL Server to handle this process by cascade delete.

Is it even possible?

0 Answers
Related