CallumVass
CallumVass

Reputation: 11456

SQLException: Multiple Cascade Paths?

I have the following model:

public class Job
{
    [Key]
    public int JobId { get; set; }
    public string Description { get; set; }
    public DateTime DateCreated { get; set; }
    public DateTime? DueDate { get; set; }
    public int JobIdentity { get; set; }
    [MaxLength(50, ErrorMessage = "Please ensure the Job Title is less than 50 characters")]
    public string JobTitle { get; set; }
    public int Priority { get; set; }

    // Relationships
    [ForeignKey("Category")]
    public int CategoryId { get; set; }
    [ForeignKey("JobType")]
    public int JobTypeId { get; set; }
    [ForeignKey("UserRequestedBy")]
    public int RequestedBy { get; set; }
    [ForeignKey("UserCreatedBy")]
    public int CreatedBy { get; set; }

    // Navigation Properties
    public virtual JobType JobType { get; set; }
    public virtual Category Category { get; set; }
    public virtual User UserRequestedBy { get; set; }
    public virtual User UserCreatedBy { get; set; }
    public virtual ICollection<Task> Tasks { get; set; }
    public virtual ICollection<JobNote> Notes { get; set; }
}

But when I try to run my application I get the following error:

Introducing FOREIGN KEY constraint 'FK_dbo.Jobs_dbo.Users_CreatedBy' on table 'Jobs' 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.

Upvotes: 1

Views: 153

Answers (1)

manadart
manadart

Reputation: 1810

With cascade delete on by default, deleting a User has two cascade paths - RequestedBy and CreatedBy

Disabling the cascade deletion is exhibited in this question.

Upvotes: 1

Related Questions