Siegeon
Siegeon

Reputation: 610

Introducing FOREIGN KEY constraint error issue

I am a bit confused as to why I am getting this error:

Introducing FOREIGN KEY constraint 'FK_QuestionTerms_Terms_TermId' 
on table 'QuestionTerms' 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.

I have a class Question and a class Term, Questions may have any number of Terms associated with them, and Terms may have any number of Questions associated with them. So I am attempting to create a many to many relationship between the two. First I attempted to use convention, and I am allowing EntityFramework to create the database. This is the Question class

public class Question
{
    public Guid Id { get; set; }
    public int QuestionNumber { get; set; }
    public string StatementHtml { get; set; }
    public string AnswerHeaderHtml { get; set; }
    public string NotesHtml { get; set; }
    public Guid CategoryId { get; set; }
    public Guid CourseId { get; set; }
    public Guid QuestionTypeId { get; set; }
    public Guid? SimulationId { get; set; }
    public Guid? SimulationTabId { get; set; }

    public ICollection<Term> Terms { get; set; }
    public ICollection<ReferenceItem> ReferenceItems { get; set; }

}

And here is the Term Class

public class Term
{
   public Guid Id { get; set; }
   public string Name { get; set; }
   public string StatementHtml { get; set; }
   public string Authority { get; set; }
   public Guid ProductId { get; set; }

   public Product Product { get; set; }
   public ICollection<Question> Questions { get; set; }
}

I have also attempted to override OnModelCreating as follows, both process result is the exact same error code.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Question>()
        .HasMany(q => q.Terms)
        .WithMany(t => t.Questions)
        .Map(x =>
            {
                x.MapLeftKey("QuestionId");
                x.MapRightKey("TermId");
                x.ToTable("QuestionTerms");
            });
} 

Upvotes: 1

Views: 10427

Answers (2)

Shiraz Bhaiji
Shiraz Bhaiji

Reputation: 65381

The problem is that a cacade delete would go back and forth between the tables.

For example first deleting term A which would delete question 1,2 and 3. Question 1 was also used in term B so term B must be deleted .....

It therefore stops you creating such constraints.

There is a good coverage of how to fix it here: Entity Framework 4.1 InverseProperty Attribute and ForeignKey

Edit

This could be a side effect of other problems. You should start with a much simpler model and then gradually build it up.

For example:

  • Why do you have both ProductId and product
  • Why CategoryId and not Category ...

Upvotes: 2

Cinchoo
Cinchoo

Reputation: 6322

Try adding it in your OnModelCreating() method

  modelBuilder.Entity<Question>().HasRequired(oo => oo.Term).WithMany(oo => oo.Questions).WillCascadeOnDelete(false);

Upvotes: 2

Related Questions