title: Entity with string for primary key, is automatically given a shadow key
body: <p>In my upgrade from .net core 3.1 to .net 6, I run in the following problem with the string primary key of my entity.</p>
  <pre class="lang-c prettyprint-override"><code>public class Country
  {
      public string CountryCode { get; set; }
      public string Name { get; set; }
  }
  </code></pre>
  <p>The entity is configured with <code>CountryCode</code> as key:</p>
  <pre class="lang-c prettyprint-override"><code>modelBuilder.Entity&lt;Country&gt;().HasKey(c =&gt; c.CountryCode);
  modelBuilder.Entity&lt;Country&gt;().Property(c =&gt; c.CountryCode).ValueGeneratedNever();
  </code></pre>
  <p>Country can be related to other entities, such as Company:</p>
  <pre class="lang-c prettyprint-override"><code>public class Company
  {
      public int Id { get; set; }
      public string CompanyCode { get; set; }
      public string Name { get; set; }
      public string CountryCode { get; set; }
      public Country Country { get; set; }
  }
  </code></pre>
  <p>With the relationship being configured as:</p>
  <pre class="lang-c prettyprint-override"><code>modelBuilder.Entity&lt;Company&gt;().HasOne&lt;Country&gt;(c =&gt; c.Country).WithMany().HasForeignKey(c =&gt; c.CountryCode);
  </code></pre>
  <p>In .net core 3.1 this worked well, however in .net 6 the <code>Country</code> entity is created with a shadow key: <code>TempId1</code>.</p>
  <pre class="lang-c prettyprint-override"><code>migrationBuilder.CreateTable(
      name: &quot;Countries&quot;,
      columns: table =&gt; new
      {
          CountryCode = table.Column&lt;string&gt;(type: &quot;nvarchar(450)&quot;, nullable: false),
          Name = table.Column&lt;string&gt;(type: &quot;nvarchar(max)&quot;, nullable: true),
          TempId1 = table.Column&lt;int&gt;(type: &quot;int&quot;, nullable: false)
      },
      constraints: table =&gt;
      {
          table.PrimaryKey(&quot;PK_Countries&quot;, x =&gt; x.CountryCode);
          table.UniqueConstraint(&quot;AK_Countries_TempId1&quot;, x =&gt; x.TempId1);
      });
  </code></pre>
  <p>From below comments by Ivan Stoev, I understand now this is related to nullable reference type functionality in c# 8.0 and above.  I tried to ensure to EF Core that the PK will not be null by ensuring CountryCode cannot be set to null in code:</p>
  <pre class="lang-c prettyprint-override"><code>public string CountryCode { get; set; } = null!;
  </code></pre>
  <p>But this had no effect on the shadow key.</p>