title: Code-first Entity Framework : foreign key - invalid object name
body: <p>I'm having trouble with Entity Framework and the code-first approach.</p>
  <p>Foreign key will not link properly</p>
  <p>Here is the error page: (<a href="https://prnt.sc/JUWTdRVCEWq7" rel="nofollow noreferrer">https://prnt.sc/JUWTdRVCEWq7</a>)</p>
  <pre><code>public class Organisme
  {
      public int ID { get; set; }
      public string Name { get; set; } = null! ;
      public string Description { get; set; } = null! ;
      public string Image { get; set; } = null! ;
  
      public Soort Soort { get; set; } = null!;
  }
  
  public class Soort
  {
      public int Id { get; set; }
      public string Name { get; set; } = null!;
      public Geslacht Geslacht { get; set; } = null!;
  
      public ICollection&lt;Organisme&gt; Organismes { get; set; } = null!;
  }
  
  
  modelBuilder.Entity&lt;Soort&gt;().HasMany(a =&gt; a.Organismes).WithOne(b =&gt; b.Soort);
  
  
  public async Task&lt;Organisme?&gt; GetByIdAsync(int id)
  {
      using OrganismeContext oc = new();
      return await oc.Organismes.AsNoTracking()
                     .Include(x =&gt; x.Soort)
                     .SingleOrDefaultAsync(x =&gt; x.ID == id);
  }
  </code></pre>
  <p>By removing <code>.Include(x =&gt; x.Soort)</code>, I no longer get the error, but the value becomes <code>null</code> which is not what I want</p>
  <p>Adding <code>.HasForeignKey(c =&gt; c.Soort)</code> gives a whole different error</p>
  <blockquote>
  <p>InvalidOperationException: 'Soort' cannot be used as a property on entity type 'Organisme' because it is configured as a navigation.</p>
  </blockquote>
  <p>Error pages: <a href="https://prnt.sc/1GL3-CgD1ruj" rel="nofollow noreferrer">https://prnt.sc/1GL3-CgD1ruj</a> and <a href="https://prnt.sc/GymTfEIhXy5x" rel="nofollow noreferrer">https://prnt.sc/GymTfEIhXy5x</a></p>