title: EF Core - One-to-many and Many-to-one relationship on same pair of entities
body: <p>I have problem defining relationship between two entities. </p>
  
  <p><strong>What I'm trying to achieve:</strong></p>
  
  <ol>
  <li><p>I wish to have some kind of media file (video/image/file) with user
  comments.</p></li>
  <li><p>User comments can also have multiple media files.</p></li>
  </ol>
  
  <p>I'm using .net core 3.1 and latest EF Core</p>
  
  <p><strong>Classes:</strong></p>
  
  <pre><code> public class Comment
  {
      public Guid Id { get; set; }
  
      // can belong to media file but in future this comment could also belong to say.. 
      // feed post, some other in-app activity
      public Guid? ParentMediaId { get; set; }
      public virtual MediaFile ParentMedia { get; set; }
  
      // can contain multiple media
      public virtual ICollection&lt;MediaFile&gt; CommentMediaCollection { get; set; } = new HashSet&lt;MediaFile&gt;();
  }
  
  public class MediaFile
  {
      public Guid Id { get; set; }
  
      // this media file can belong to comment, but also number of other entities in app
      public Guid? ParentCommentId { get; set; }
      public virtual Comment ParentComment { get; set; }
  
      // can also have multiple comments
      public virtual ICollection&lt;Comment&gt; Comments { get; set; } = new HashSet&lt;Comment&gt;();
  }
  </code></pre>
  
  <p><strong>Configuration files:</strong></p>
  
  <p>Comment configuration file</p>
  
  <pre><code>            builder
              .HasMany(x =&gt; x.CommentMediaCollection)
              .WithOne(x =&gt; x.ParentComment)
              .HasForeignKey(x =&gt; x.ParentCommentId)
              .OnDelete(DeleteBehavior.Cascade);
  
          builder
              .HasOne(x =&gt; x.ParentMedia)
              .WithMany(x =&gt; x.Comments)
              .HasForeignKey(x =&gt; x.ParentMediaId)
              .OnDelete(DeleteBehavior.SetNull)
              .IsRequired(false);
  </code></pre>
  
  <p>Media configuration file:</p>
  
  <pre><code>            builder
              .HasOne(x =&gt; x.ParentComment)
              .WithMany(x =&gt; x.CommentMediaCollection)
              .HasForeignKey(x =&gt; x.ParentCommentId)
              .OnDelete(DeleteBehavior.SetNull)
              .IsRequired(false);
  
          builder
              .HasMany(x =&gt; x.Comments)
              .WithOne(x =&gt; x.ParentMedia)
              .HasForeignKey(x =&gt; x.ParentMediaId)
              .OnDelete(DeleteBehavior.Cascade);
  </code></pre>
  
  <p><strong>Problem:</strong></p>
  
  <pre><code>Unable to determine the relationship represented by navigation property 'Comment.ParentMedia' of type 'MediaFile'. 
  Either manually configure the relationship, or ignore this property using 
  the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
  </code></pre>
  
  <p>Followed number of suggestions online but all boil down to configuration which to me... seems fine?</p>
  
  <p>Cheers!</p>