title: One-to-many relationship on custom columns
body: <p>I have two models that I'd like to specify a one-to-many relationship between, but I can't seem to get it to work.  Here are the models:</p>
  <pre class="lang-cs prettyprint-override"><code>    public class Apple
      {
          public int Id { get; set; }
          public int ThingId { get; set; }
  
          public List&lt;Banana&gt; Bananas { get; set; }
      }
      public class Banana
      {
          public int Id { get; set; }
          public int ThingId { get; set; }
   
          [ForeignKey(&quot;ThingId&quot;)]
          public Apple Apple { get; set; }
      }
  </code></pre>
  <p>As you can see, both class <code>Apple</code> and class <code>Banana</code> have a column in them called <code>ThingId</code> and that's how I'd like to join the associations together.  This is a legacy DB so I can't change the column names.  However, when I run a query like this, I don't get an array of <code>B</code> objects back even though I know they exist.</p>
  <pre class="lang-cs prettyprint-override"><code>var thing = _context.Apples.Include(a =&gt; a.Bananas).FirstOrDefault();
  // thing - properly populated but the &quot;Bananas&quot; association is null
  </code></pre>
  <p>Any ideas on how I can get this to work properly using a custom column like this (preferably without fluent syntax since I prefer attributes, but not a big deal if that's necessary).</p>
  <p>Thanks!</p>
  <p><strong>Edit:</strong>
  The link in the answer below by Rafi did the trick.  Here's what I ending up using via the Fluent API.</p>
  <pre class="lang-cs prettyprint-override"><code>protected override void OnModelCreating(ModelBuilder modelBuilder)
  {
      modelBuilder.Entity&lt;Banana&gt;()
          .HasOne(b =&gt; b.Apple)
          .WithMany(a =&gt; a.Bananas)
          .HasForeignKey(a =&gt; a.ThingId)
          .HasPrincipalKey(b =&gt; b.ThingId);
  }
  </code></pre>