title: Entity Framework Cascading Delete
body: <p><em>First of all, apologies if I'm missing some basic stuff here but I'm new to EF and still getting my head around setting up the DB code first....</em></p>
  
  <p>I'm having a similar problem to this <a href="https://stackoverflow.com/questions/19373310/introducing-foreign-key-constraint-may-cause-cycles-or-multiple-cascade-paths">Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths</a> but can't seem to work out from the comments there what I need to do with my particular model. When I attempt to update database after adding in <code>public virtual Actor actor { get; set; }</code> to my UseCase class, I get this error: </p>
  
  <pre><code>Introducing FOREIGN KEY constraint 'FK_dbo.UseCase_dbo.Actor_ActorID' on table 'UseCase' 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.
  </code></pre>
  
  <p>I know it must be something to do with the way that my FK constraints are set up (probably something to do with deleting a use case meaning that I'll end up deleting data from multiple other tables). </p>
  
  <p>I tried turning off cascading delete, but still get the error: </p>
  
  <pre><code>protected override void OnModelCreating(DbModelBuilder modelBuilder)
          {
              //prevent table names created by entity framework being pluralised
              modelBuilder.Conventions.Remove&lt;PluralizingTableNameConvention&gt;();
              //Turn off delete cascades between parent child tables. May need to put this back in future, but for the time being it is stopping the database being updated through the package manager console (error is that a foregin key constraint may cause cycles or multiple cascade paths) 
              modelBuilder.Conventions.Remove&lt;OneToManyCascadeDeleteConvention&gt;();
              modelBuilder.Conventions.Remove&lt;ManyToManyCascadeDeleteConvention&gt;();
          }
  </code></pre>
  
  <p>Here are my models. What should happen is that only if a project is deleted should it's use cases or actors be deleted. Actors should not be deleted when a UseCase is, because they may be involved in other UseCases. Can anyone point to what I need to change? </p>
  
  <p>Finally, the correct model indeed is this <code>Project &gt; Actors &gt; Use Cases</code>. I assume that I should just remove  <code>public virtual int ProjectID { get; set; }</code> and <code>public virtual Project project { get; set; }</code> from UseCase? </p>
  
  <p>Learning hurts! </p>
  
  <pre><code> public class Project
      {
          public virtual int ID {get; set;}
          [DisplayName ("Project Name")]
          public virtual string ProjectName { get; set; }
          [DisplayName("Client")]    
          public virtual string ClientID { get; set; }
          public virtual string Description { get; set; }
          [DisplayName("Use Cases")]
          public virtual ICollection &lt;UseCase&gt; UseCases { get; set; } 
  
     }
  
  public class UseCase
  
  {
      public virtual int ID { get; set; }
      [Required]
      public virtual int ProjectID { get; set; }
      public virtual int ActorID { get; set; }
      [Required]
      public virtual Actor actor { get; set; }
      public virtual string Title { get; set; }
      public virtual Level? Level { get; set; }
      public virtual string Precondition { get; set; }
      public virtual string MinimalGuarantee { get; set; }
      public virtual string SuccessGuarantee { get; set; }
      public virtual ICollection&lt;Step&gt; Steps { get; set; }
      public virtual ICollection&lt;Extension&gt; Extensions { get; set; }
      public virtual ICollection&lt;Query&gt; Queries { get; set; }
  
  }
  
  public class Actor
  {
      public virtual int ID { get; set; }
      public virtual int projectID { get; set; }
      public virtual Project project { get; set; } 
      public virtual string Title { get; set; }
  
      [DataType(DataType.MultilineText)]
      public virtual string Description { get; set; }
  
  }
  </code></pre>
  
  <p><strong>UPDATED</strong> So, here is my modified code based on feedback below. I'm still getting the same error, either when I run the application and it tries to create the DB or when I try to update the database through package manager <code>Update-Database</code>. Driving me crazy.  </p>
  
  <p>To me, the code below says if I delete an actor, delete the use cases for that actor too. If I delete a project, delete the actors for the project and therefore delete the use cases for each actor too. But if I delete a project, don't delete the use cases. Clearly, I'm misunderstanding something quite badly :-( </p>
  
  <pre><code>    modelBuilder.Entity&lt;Actor&gt;()
      .HasMany(a =&gt; a.useCases)
      .WithRequired(uc =&gt; uc.actor)
      .HasForeignKey(uc =&gt; uc.ActorID)
      .WillCascadeOnDelete(true); // and this works
  
      modelBuilder.Entity&lt;Project&gt;()
      .HasMany(p =&gt; p.actors)
      .WithRequired(a =&gt; a.project)
      .HasForeignKey(a =&gt; a.projectID)
      .WillCascadeOnDelete(true); // this works
  
      modelBuilder.Entity&lt;Project&gt;()
      .HasMany(p =&gt; p.UseCases)
      .WithRequired(uc =&gt; uc.project)
      .HasForeignKey(uc =&gt; uc.ProjectID)
      .WillCascadeOnDelete(false); // disable this cascading delete
  </code></pre>