0

I'm trying to create a many-to-many relationship between my two tables, but when I run Update-Database command I get this error:

Introducing FOREIGN KEY constraint 'FK_dbo.ExamQuestions_dbo.Questions_Question_Id' on table 'ExamQuestions' 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.

My first entity is :

public class Question
{
   public Question()
   {
       this.Exams = new HashSet<Exam>();
   }

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

   [Required(ErrorMessage="Question is Required")]
   [Display(Name="Question")]
   [AllowHtml]
   public string QuestionText { get; set; }

   // public bool IsMultiSelect { get; set; }
   public string Hint { get; set; }
   public string HelpLink { get; set; }

   public int Marks { get; set; }
   public byte[] ImageData { get; set; }
   [StringLength(50)]
   public string MimeType { get; set; }
   public byte[] Audio { get; set; }

   public int QuestionTypeId { get; set; }
   public int TopicId { get; set; }
   public int DifficulityLevelId { get; set; }
   public int SubjectId { get; set; }

   public DifficultyLevel QuestionDifficulity { get; set; }
   public Topic Topic { get; set; }

   public virtual ICollection<Option> Options { get; set; }
   public ICollection<Exam> Exams { get; set; }
}

And the second entity is:

public class Exam
{
   public Exam()
   {
       this.Questions = new HashSet<Question>();
   }

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

   [Required]
   public string Name { get; set; }
   [Required]
   public int Duration { get; set; }
   [Required]
   public int TotalQuestion { get; set; }
   [Required]
   public int TotalMarks { get; set; }
   public bool SectionWiseTime { get; set; }
   public bool QuestionWiseTime { get; set; }
   public bool AllQustionRequired { get; set; }
   public bool AllowBackForward { get; set; }
   public bool SuffleSubjectWise { get; set; }
   public bool SuffleOptionWise { get; set; }
   public bool GroupSubjectWise { get; set; }

   [Required]
   public int ExamTypeId { get; set; }
   [Required]
   public int ExamInstructionId { get; set; }
   [Required]
   public int DifficultyLevelId { get; set; }

   public virtual ExamType ExamType { get; set; }
   public virtual ExamInstruction ExamInstruction { get; set; }
   public virtual DifficultyLevel DifficultyLevel { get; set; }

   public virtual  ICollection<Question> Questions { get; set; }
   //public virtual ICollection<ExamSchedule> ExamSchedules { get; set; }
}

Can someone tell me where I'm going wrong?

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388

1 Answers1

0

By default, EF has cascading deletes set. This error is warning you that this can cause cascading deletes with many to many relationships. And is probably not what you intend to have happen on a delete/update.

You can remove the OneToManyCascadeDeleteConvention in the OnModelCreating method, or on the fluent mapping for each entity.

Details are provided in this SO Answer

bigtlb
  • 1,462
  • 9
  • 16