Entity Framework ошибка отношения многих ко многим - PullRequest
0 голосов
/ 11 мая 2018

Я пытаюсь создать отношение «многие ко многим» между двумя моими таблицами, но когда я запускаю команду Update-Database, я получаю эту ошибку:

Введение ограничения FOREIGN KEY 'FK_dbo.ExamQuestions_dbo.Questions_Question_Id 'в таблице' ExamQuestions 'может вызывать циклы или несколько каскадных путей.Укажите ON DELETE NO ACTION или ON UPDATE NO ACTION или измените другие ограничения FOREIGN KEY.
Не удалось создать ограничение или индекс.См. Предыдущие ошибки.

Моя первая сущность:

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; }
}

А вторая сущность:

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; }
}

Может кто-нибудь сказать мне, где яидет не так?

1 Ответ

0 голосов
/ 21 мая 2018

По умолчанию в EF установлено каскадное удаление.Эта ошибка предупреждает вас, что это может привести к каскадному удалению со многими отношениями.И, вероятно, это не то, что вы намеревались произойти при удалении / обновлении.

Вы можете удалить OneToManyCascadeDeleteConvention в методе OnModelCreating или в текущем отображении для каждой сущности.

Подробности приведены в этом SO Ответе

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...