title: Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths. Specify on delete no action
body: <p>User, Employer, Candidates and Job, an employer can create multiple jobs and each job can only have one employer, a candidate can apply for many jobs and each job can have multiple applied members.</p>
  
  <p>So the relationship is like this:</p>
  
  <p><img src="https://i.stack.imgur.com/iyaUB.png" alt="enter image description here"></p>
  
  <p>I am using entity framework code first approach, at the moment if I delete an employer, it is going to delete all related jobs and the user from database, and if I delete candidate, it is going to delete the user:</p>
  
  <pre><code>modelBuilder.Entity&lt;Employer&gt;()
       .HasRequired(e =&gt; e.User)
      .WithOptional(e =&gt; e.Employer).WillCascadeOnDelete();
  
  //member is candidate
  modelBuilder.Entity&lt;Member&gt;()
      .HasRequired(e =&gt; e.User)
      .WithOptional(e =&gt; e.Member).WillCascadeOnDelete();
  
  modelBuilder.Entity&lt;Employer&gt;()
      .HasMany(a =&gt; a.Jobs)
      .WithRequired(b =&gt; b.Employer)
      .WillCascadeOnDelete();
  </code></pre>
  
  <p>Everything works fine except when I specify many to many relationship between candidates and job and update the database using "update-database", it gives me this error:</p>
  
  <p><strong>Introducing FOREIGN KEY constraint 'FK_dbo.MemberJobMap_dbo.Jobs_JobId' on table 'MemberJobMap' 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.</strong></p>
  
  <p>Here is how I specified many to many relationship:</p>
  
  <pre><code>modelBuilder.Entity&lt;Member&gt;()
     .HasMany(m =&gt; m.Jobs)
     .WithMany(j =&gt; j.Members)
     .Map(c =&gt;
     {
        c.MapLeftKey("Id");
        c.MapRightKey("JobId");
        c.ToTable("MemberJobMap");
     });
  </code></pre>
  
  <p>and when I add migration:</p>
  
  <pre><code>CreateTable(
     "dbo.MemberJobMap",
     c =&gt; new
     {
        Id = c.String(nullable: false, maxLength: 128),
        JobId = c.Int(nullable: false),
     })
     .PrimaryKey(t =&gt; new { t.Id, t.JobId })
     .ForeignKey("dbo.Members", t =&gt; t.Id, cascadeDelete: true)
     .ForeignKey("dbo.Jobs", t =&gt; t.JobId, cascadeDelete: true)
     .Index(t =&gt; t.Id)
     .Index(t =&gt; t.JobId);   
  </code></pre>
  
  <p>I tried changing cascadeDelete to false but that gives me error when I delete a candidate that have applied jobs or when I try to delete a job with applied candidates.</p>
  
  <p>How to fix this error? So that:</p>
  
  <ol>
  <li>When a job is removed, it is going to remove associated
  candidatejobmap table rows without effecting any other table </li>
  <li>When a candidate is removed, it is going to remove associated
  candidatejobmap table rows and user table row without effecting any
  other table </li>
  <li>While keeping all other specified cascade delete action
  the same</li>
  </ol>