0
votes

I'm trying to update-database and got this error. I've been wrestling with this for a while and can't quite figure out what's happening.

Introducing FOREIGN KEY constraint 'FK_dbo.Comments_dbo.Users_UserId' on table 'Comments' 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.

Here is my User class:

public class User
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public string Email { get; set; }
    [Required]
    public UserType ProfileType { get; set; }
    [Required]
    public string PasswordHash { get; set; }
    [Required]
    public int Location { get; set; }
    public int? SessionTokenId { get; set; }

    public SessionToken SessionToken { get; set; }
    public List<Comment> Comments { get; set; }
}

And here is my Comment class:

public class Comment
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    [Required]
    public int UserId { get; set; }
    [Required]
    public int ArticleId { get; set; }
    [Required]
    public string Message { get; set; }
    [Required]
    public DateTime PostDate { get; set; }

    public User User { get; set; }
    public Article Article { get; set; }
}

I've tried to add some:

modelBuilder.Entity<Comment>()
            .HasRequired(u => u.User)
            .WithMany()
            .WillCascadeOnDelete(false);

I think it's wrong, and I need to delete all user comments on user deleting.

But it gives no result. What I'm wrote wrong?

1

1 Answers

0
votes

I suppose you have one more dependency, for example it could be between Article and User and in this case you must apply your fix for this too.