title: How to set on delete cascade for self reference Foreign Key in Entity Framework in ASP.NET
body: <p>I am developing an ASP.NET MVC project. I am using Entity Framework code first approach to interact with database. But I am having a problem with setting on cascade delete for self-reference foreign key for an entity. </p>
  
  <p>This is my entity class with self reference foreign key</p>
  
  <pre><code>public class Category
      {
          public int Id { get; set; }
          [Required]
          [MaxLength(50)]
          public string Name { get; set; }
          [MaxLength(55)]
          public string MmName { get; set; }
          public int? ParentId { get; set; }
  
          [ForeignKey("ParentId")]
          public virtual Category ParentCategory { get; set; }
          public virtual ICollection&lt;Category&gt; Categories { get; set; }
      }
  </code></pre>
  
  <p>This is my context</p>
  
  <pre><code>public class StoreContext : DbContext
      {
          public StoreContext():base("DefaultConnection")
          {
  
          }
  
          public DbSet&lt;Category&gt; Categories { get; set; }
  
          protected override void OnModelCreating(DbModelBuilder modelBuilder)
          {
              modelBuilder.Entity&lt;Category&gt;().HasOptional(x =&gt; x.ParentCategory).WithMany(c =&gt; c.Categories).WillCascadeOnDelete();
          }
      }
  </code></pre>
  
  <p>When I run, multiple cascade path errors throw</p>
  
  <pre><code>Introducing FOREIGN KEY constraint 'FK_dbo.Categories_dbo.Categories_ParentId' on table 'Categories' 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>Is it possible set on delete cascade for self-referencing foreign key in entity framework code first? </p>