title: Defining Self Referencing Foreign-Key-Relationship Using Entity Framework 7 Code First
body: <p>I have an <code>ArticleComment</code> entity as you can see below:</p>
  
  <pre><code>public class ArticleComment
   {
      public int ArticleCommentId { get; set; }
      public int? ArticleCommentParentId { get; set; }
  
      //[ForeignKey("ArticleCommentParentId")]
      public virtual ArticleComment Comment { get; set; }
      public DateTime ArticleDateCreated  { get; set; }
      public string ArticleCommentName { get; set; }
      public string ArticleCommentEmail { get; set; }
      public string ArticleCommentWebSite { get; set; }
      public string AricleCommentBody { get; set; }
  
      //[ForeignKey("UserIDfk")]   
      public virtual ApplicationUser ApplicationUser { get; set; }
      public Guid? UserIDfk { get; set; }
  
      public int ArticleIDfk { get; set; }
      //[ForeignKey("ArticleIDfk")]   
      public virtual Article Article { get; set; }
  
  
  }
  </code></pre>
  
  <p>I want to define a foreign key relationship in such a way that one comment can have many reply or child, I've tried to create the relationship using fluent API like this:</p>
  
  <pre><code>builder.Entity&lt;ArticleComment&gt;()
              .HasOne(p =&gt; p.Comment)
              .WithMany()
              .HasForeignKey(p =&gt; p.ArticleCommentParentId)
              .OnDelete(DeleteBehavior.Restrict)
              .IsRequired(false);
  </code></pre>
  
  <p>I followed the solution that was proposed <a href="http://roland.kierkels.net/c-asp-net/how-to-configure-a-self-referencing-entity-in-code-first/" rel="nofollow">here</a> and <a href="https://github.com/aspnet/EntityFramework/issues/1467" rel="nofollow">here</a>, but I get an error with the message:</p>
  
  <blockquote>
    <p>Introducing FOREIGN KEY constraint 'FK_ArticleComment_ArticleComment_ArticleCommentParentId' on table 'ArticleComment' 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 or index. See previous errors.</p>
  </blockquote>
  
  <p>First I though by setting the <code>OnDelete(DeleteBehavior.Restrict)</code> this would go away, but the problem persist, also I've tried to use the data annotation <code>[ForeignKey("ArticleCommentParentId")]</code> as you can see the commented code in the <code>ArticleComment</code> definition, but it didn't work, I'd appreciate any though on this.</p>