0

I have a many-to-many relationship between three tables as shown.

enter image description here

when I apply migration to build and seed the phisical database I get that error

Introducing FOREIGN KEY constraint 'FK_Rooms_postSubjects_postSubjectId' on table 'Rooms' 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.

these three tables are implemented using Entity Framework Models and seeded to be cascaded on deletion as follows

First: the three models (PostSubject, Room, AppUser)

PostSubject

public class PostSubject
{
    [Key]
    public Guid Id { get; set; }
    
    // ... some props

    public virtual List<Room> rooms { get; set; }

    public string authorId { get; set; }
    [ForeignKey("authorId")]
    public virtual AppUser author { get; set; }
}

Room

public class Room
{
    public Guid Id { get; set; }

    #nullable enable
    public Guid? postSubjectId { get; set; }
    [ForeignKey("postSubjectId")]
    public PostSubject? postSubject { get; set; }

    #nullable enable
    public string? MemberId { get; set; }
    [ForeignKey("MemberId")]
    public AppUser? Member { get; set; }
}

AppUser

public class AppUser: IdentityUser
{
    // ... some props

    public List<Room> rooms { get; set; }
    public List<PostSubject> postSubjects { get; set; }
}

Second: the seeding logic to cascade deletion

// delete one postSubject should be followed with deleting all related rooms
builder.Entity<Room>().HasOne(i => i.postSubject)
.WithMany(c => c.rooms)
.OnDelete(DeleteBehavior.Cascade);

// delete one user should be followed with deleting all related rooms
builder.Entity<Room>().HasOne(i => i.Member) 
.WithMany(c => c.rooms)
.OnDelete(DeleteBehavior.Cascade);

// delete one user should be followed with deleting all related postSubjects
builder.Entity<PostSubject>().HasOne(i => i.author)
.WithMany(c => c.postSubjects) // IsRequired means it's necessary to have the relation
.OnDelete(DeleteBehavior.Cascade);
MOHAMED ABUELATTA
  • 305
  • 2
  • 5
  • 15
  • There's no circular relation in that diagram. Which isn't an actual table schema either. – Panagiotis Kanavos Aug 25 '21 at 11:07
  • It's not about the title. You get that error because the actual relations are *not* those in the diagram. Where are the `AppUser` relations? – Panagiotis Kanavos Aug 25 '21 at 11:10
  • The issue is not with EF, this is a restriction of MS SQL. You cannot have two foreign keys on a table with ON CASCADE DELETE. – juunas Aug 25 '21 at 11:11
  • @juunas mmmm do you mean cascade delete can be applied just in case of One to Many? for example can I make cascade between PostSubject and Room and set null for AppUser and Room? – MOHAMED ABUELATTA Aug 25 '21 at 11:16
  • One of the Cascades has to be set to another option. Set null could work. – juunas Aug 25 '21 at 11:38
  • @juunas thanks bro I removed the second one between Room and AppUser "Member" and it works. using setNull or NoAction didn't work however if I need to delete a user and their related rooms I would use triggers instead as Panagiotis Kanavos has mentioned. – MOHAMED ABUELATTA Aug 25 '21 at 21:23

0 Answers0