2

I have a table that should refer to the same table. But when I try to update the database schema, it throws me this error:

When i try to update the schema, PMC throws this error:

System.Data.SqlClient.SqlException (0x80131904): Introducing FOREIGN KEY constraint 'FK_Directory_Directory_SubOfId' on table 'Directory' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

I tried setting ON DELETE to CASCADE but nothing, still the same error. I do not know how to set NO ACTION because mapping does not offer this option.

What with this?

Entity:

public class Directory : BaseEntity
{
    public int ID { get; set; }

    public int? SubOfId { get; set; }

    [ForeignKey("SubOfId")]
    public Directory SubOf { get; set; }

    public virtual ICollection<ImageDirectory> ImageDirectory { get; set; }
}

Model builder:

 protected override void OnModelCreating(ModelBuilder builder)
        {

            base.OnModelCreating(builder);

...

            builder.Entity<Directory>().HasOne(e => e.SubOf).WithOne().HasForeignKey<Directory>(a => a.SubOfId).OnDelete(DeleteBehavior.Cascade);

        }
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
ZombieBot
  • 127
  • 1
  • 8
  • Possible duplicate of [What is the syntax for self referencing foreign keys in EF Code First?](https://stackoverflow.com/questions/4811194/what-is-the-syntax-for-self-referencing-foreign-keys-in-ef-code-first) – Panagiotis Kanavos Mar 06 '18 at 12:41
  • `DeleteBehavior.Restrict` ? – Tewr Mar 06 '18 at 12:41
  • There are quite a few duplicates. Did you check them? – Panagiotis Kanavos Mar 06 '18 at 12:42
  • Another one [Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths - why?](https://stackoverflow.com/questions/17127351/introducing-foreign-key-constraint-may-cause-cycles-or-multiple-cascade-paths) – Panagiotis Kanavos Mar 06 '18 at 12:43
  • Try "HasOptional" at the place of HasOne in "builder.Entity().HasOne" – Milan Raval Mar 06 '18 at 12:53
  • `DeleteBehavior.Restrict` is what you need. See also https://stackoverflow.com/questions/48521939/efcore-nullable-relationship-setting-ondelete-referentialaction-restrict/48523214#48523214 and the documentation. – Ivan Stoev Mar 06 '18 at 13:13
  • Thanks. It has additional problem with migrations. – ZombieBot Mar 06 '18 at 13:35
  • @MilanRaval HasOptional doesnt work in EF Core. Or? – ZombieBot Mar 06 '18 at 13:36
  • @ZombieBot or you should update the question and provide all the information there, and specify *what* the problem is - updating as you said or migrations which makes more sense? Or both? "doesn't work" isn't an error message or compilable code. – Panagiotis Kanavos Mar 06 '18 at 14:04
  • @ZombieBot also *read* the duplicates. As the second duplicate explains a required relation creates cascading deletes on both sides. You need to make one side optional or disable cascading deletes. That means, *don't* use DeleteBehavior.Cascade. Check one of [the other options](https://learn.microsoft.com/en-us/ef/core/saving/cascade-delete) although the default ClientSetNull should work – Panagiotis Kanavos Mar 06 '18 at 14:12

1 Answers1

2

This exception isn't due to self-referencing. You get this when an entity can be deleted via multiple cascade paths. Based on the code you've provided, my best guess is that something going on with ImageDirectory and relationships in play there is actually the source of the issue.

Long and short, you need to investigate your object graph to see where removing one entity type might cause multiple cascades. Then, you'll need to shut off some of those cascades to proceed. There's not much more that can be said to help you, unfortunately, without being able to see all your entities and the relationships between them all.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444