'Entity Framework Core Delete Behavior

So I have these 3 tables where 2 of them(Post(Parent of Comment table as well) and Comment Table) is parent of Reaction Table.

Post Table:

public class Post
{
    public long Id { get; set; }
    [MaxLength(800)]
    public string Message { get; set; }
    public string FilePath { get; set; }
    [Required]
    public short Audience { get; set; }
    [Required]
    public bool AreCommentsAllowed { get; set; }
    public DateTime DatePosted { get; set; }
    public ICollection<Comment> Comments { get; set; }
    public ICollection<Reaction> Reactions { get; set; }
    public int? GroupId { get; set; }
    public Group Group { get; set; }
    [Required]
    public string UserId { get; set; }
    public User User { get; set; }
}

Comment Table:

 public class Comment
{
    public long Id { get; set; }
    [MaxLength(800)]
    public string Message { get; set; }
    public string FilePath { get; set; }
    [Required]
    public DateTime DateCommented { get; set; }
    public ICollection<Reaction> Reactions { get; set; }
    public long? PostId { get; set; }
    public Post Post { get; set; }
    public long? ReplyId { get; set; } // For replies on this comment
    [Required]
    public string UserId { get; set; }
    public User User { get; set; }
}

Reaction Table:

public class Reaction
{
    public long Id { get; set; }
    [Required]
    public short ReactionTypeId { get; set; }
    public ReactionType ReactionType { get; set; }
    public DateTime DateReacted { get; set; }
    public long? PostId { get; set; }
    public Post Post { get; set; }
    public long? CommentId { get; set; }
    public Comment Comment { get; set; }
    [Required]
    public string UserId { get; set; }

    public User User { get; set; }
}

What I want to happen is when I delete the post these records reactions, comments and comment reactions will be deleted as well. To achieve this behavior I used these configuration on fluent API.

builder.Entity<Post>()
            .HasMany(x => x.Comments)
            .WithOne(x => x.Post)
            .OnDelete(DeleteBehavior.Cascade);


        builder.Entity<Post>()
            .HasMany(x => x.Reactions)
            .WithOne(x => x.Post)
            .OnDelete(DeleteBehavior.Cascade);

        builder.Entity<Comment>()
            .HasMany(x => x.Reactions)
            .WithOne(x => x.Comment)
            .OnDelete(DeleteBehavior.Cascade);

However, after running the update-database it shows the error : Introducing FOREIGN KEY constraint 'FK_Reactions_Posts_PostId' on table 'Reactions' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.



Solution 1:[1]

This is because when you delete a Post you have two paths where you would delete a Reaction (in the relationship with Posts and with the Relationship with Comments which will also be deleted). Just set one of the Relationships DeleteBehavior to NoAction to fix this.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Carles Company