title: Setting custom foreign key column name with private field with EFCore
body: <p>I have the following data model:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class Foo
  {
      public Foo(int barId)
      {
          BarId = barId;
      }
  
      private int BarId;
      public Bar Bar { get; private set; }
  }
  
  public class FooTypeConfig : IEntityTypeConfiguration&lt;Foo&gt;
  {
      public void Configure(EntityTypeBuilder&lt;Foo&gt; builder)
      {
          builder.HasOne(x =&gt; x.Bar)
              .WithMany()
              .HasForeignKey("BarId");
      }
  }
  
  public class Bar
  {
      public int Id { get; private set; }
  }
  </code></pre>
  
  <p>This works great and according to my expectations, I have a <code>Foo</code> table containing <code>Id</code> and <code>BarId</code>. My private field <code>BarId</code> and my <code>Bar</code> property are also correctly materialized when reading <code>Foo</code> from the database.</p>
  
  <p>The problem is that I would like to find a way to name my private field, and choose a different name for my database column. I would like to name my property <code>_barId</code> and still choose <code>BarId</code> as my column name in my database.</p>
  
  <p><strong>Is this possible?</strong></p>
  
  <hr>
  
  <p>I have tried renaming the field in my <code>Foo</code> class and specifying my (now non-conventionally-named) foreign key <code>_barId</code> in my <code>EntityTypeConfiguration</code></p>
  
  <pre class="lang-cs prettyprint-override"><code>builder.HasOne(x =&gt; x.Bar).WithMany().HasForeignKey("_barId");
  </code></pre>
  
  <p>But this resulted in EF still generating a <code>BarId</code> column, without using it as foreign key to the <code>Bar</code> table...</p>
  
  <pre class="lang-cs prettyprint-override"><code>migrationBuilder.CreateTable(
      name: "Foos",
      columns: table =&gt; new
      {
          Id = table.Column&lt;int&gt;(nullable: false)
              .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
          BarId = table.Column&lt;int&gt;(nullable: true),
          _barId = table.Column&lt;int&gt;(nullable: false)
      },
      constraints: table =&gt;
      {
          table.PrimaryKey("PK_Foos", x =&gt; x.Id);
          table.ForeignKey(
              name: "FK_Foos_Bars__barId",
              column: x =&gt; x._barId,
              principalTable: "Bars",
              principalColumn: "Id",
              onDelete: ReferentialAction.Cascade);
      });
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/FC2D5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FC2D5.png" alt="Foo table structure"></a></p>