0

I'm trying to create my database from my models, but I keep getting the error Introducing FOREIGN KEY constraint 'FK_dbo.Reports_dbo.UserProfiles_UserId' on table 'Reports' 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. See previous errors.

anyone knows what might be wrong with my models/setup?

These are all used Models

public class Report {
    [Key]
    public int Id { get; set; }

    [Required]
    public string Number { get; set; }
    public bool Synced { get; set; }

    public DateTime CreationDate { get; set; }

    public int NewCommentId { get; set; }
    public virtual Comment NewComment { get; set; }

    public int UserId { get; set; }
    public virtual UserProfile User { get; set; }

    public virtual ICollection<Comment> Comments { get; set; }
    public virtual ICollection<Photo> PhotosBefore { get; set; }
    public virtual ICollection<Photo> PhotosAfter { get; set; }
}
public class Photo {
    [Key]
    public int Id { get; set; }
    public string Image { get; set; }
    public bool Synced { get; set; }

    public DateTime CreationDate { get; set; }

    public int ReportId { get; set; }
    public virtual Report Report { get; set; }

    public int UserId { get; set; }
    public virtual UserProfile User { get; set; }
}
public class Comment {
    [Key]
    public int Id { get; set; }

    public DateTime CreationDate { get; set; }
    public string Text { get; set; }

    public int ReportId { get; set; }
    public virtual Report Report { get; set; }

    public int UserId { get; set; }
    public virtual UserProfile User { get; set; }
}
public class UserProfile {
    [Key]
    public int Id { get; set; }
    public string Stamnummer { get; set; }
    public string Leverancier { get; set; }


    public virtual ICollection<Comment> Comments { get; set; }
    public virtual ICollection<Report> Reports { get; set; }
    public virtual ICollection<Photo> Photos { get; set; }
}
Steve Greene
  • 12,029
  • 1
  • 33
  • 54
Kiwi
  • 2,713
  • 7
  • 44
  • 82

1 Answers1

0

In order to be certain, we need to see how you have configured your relationships using the model builder in the OnModelCreating method. Based on the error message you have provided, it appears that you have relationships configured so that one of your entities is configured for cascade on delete from two or more other entities.

As an example (this may not be the case, but rather just a means of describing the problem):

User has a one-to-many relationship with Comments

User has a one-to-many relationship with Reports

Report has a one-to-many relationship with Comments

Comment is configured so that a User is required

Comment is configured so that a Report is required

Report is configured so that a User is required

Any one-to-many relationship where the entity one side of the relationship is required is going to have cascade on delete configured by default. In this scenario, if a User were deleted it would trigger a cascade to both Reports and Comments. Each Report would also cause a cascade on Comments.

The solution is to disable cascading deletes for one of the relationships. You can find a similar question to yours here describing what I mentioned above.

Community
  • 1
  • 1
Larry
  • 511
  • 4
  • 10