title: In EF Core, how do I configure the foreign key for reference owned types using Fluent API
body: <p>In a <code>Company</code> entity I have included <code>Address</code> as a reference <a href="https://docs.microsoft.com/en-us/ef/core/modeling/owned-entities" rel="nofollow noreferrer">owned type</a> (so that the <code>Company</code> table includes the properties of the address as columns).  The reference owned <code>Address</code> includes <code>Country</code> by holding the foreign key <code>CountryCode</code> which is a property of the <code>Address</code> class.  As such I need to configure this property as the foreign key.  </p>
  
  <p>When I use the attribute <code>ForeignKey("Country")</code> the migration is successful and the table is created with the correct column as FK: <code>[Companies].[Address_CountryCode]</code>.  However I want to use the Fluent API for all my EF Core DbContext configurations.  And this fails as the migration finds a conflict in ownership of <code>Address</code>.</p>
  
  <pre class="lang-c prettyprint-override"><code>class Company
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public Address Address { get; set; }
  }
  
  public class Address
  {
      public string City { get; set; }
      public string Street { get; set; }
      public string CountryCode { get; set; }
      public Country Country { get; set; }
  }
  </code></pre>
  
  <pre class="lang-c prettyprint-override"><code>modelBuilder.Entity&lt;Company&gt;().OwnsOne&lt;Address&gt;(c =&gt; c.Address);
  modelBuilder.Entity&lt;Address&gt;().HasOne&lt;Country&gt;(c =&gt; c.Country).WithMany().HasForeignKey(a =&gt; a.CountryCode);    
  </code></pre>
  
  <p>Setting up the foreign key through Fluent API in this matter, fails with the following message:
  <code>The type 'Address' cannot be configured as non-owned because an owned entity type with the same name already exists.</code>.  Again, with the <code>ForeignKey</code> attribute it works as expected.</p>
  
  <p>How do I configure this reference owned type relationship in Fluent API correctly?</p>