title: Influencing foreign key column naming in EF code first (CTP5)
body: <p>I have a POCO class that has two one-way unary relationships with another class, both classes share an ancestor.  The names of the foreign keys in the generated schema do not reflect the property names.  (Properties MainContact and FinancialContact give PersonId and PersonId1 field names).</p>
  
  <p>How can I influence schema generation to generate database column names that match the property names?</p>
  
  <p>The model looks like this:</p>
  
  <p><img src="https://i.stack.imgur.com/e7l3i.png" alt="The class model"></p>
  
  <p>The code looks like this:</p>
  
  <pre><code>public class CustomerContext: DbContext
  {
     public DbSet&lt;Organisation&gt; Organisations { get; set; }
     public DbSet&lt;Person&gt; Persons { get; set; }
  
     protected override void OnModelCreating(ModelBuilder builder)
     {
        DbDatabase.SetInitializer(new DropCreateDatabaseAlways&lt;CustomerContext&gt;());
     }
  }
  
  public abstract class Customer
  {
     public int Id { get; set; }
     public string Name { get; set; }
  }
  
  public class Person : Customer
  {
     public string Email { get; set; }
  }
  
  public class Organisation : Customer
  {
     public Person FinancialContact { get; set; }
     public Person MainContact { get; set; }
  }
  </code></pre>
  
  <p>The schema looks like this:
  <img src="https://i.stack.imgur.com/hMw7B.png" alt="enter image description here"></p>
  
  <h1>Answer from druttka</h1>
  
  <hr>
  
  <p>druttka's answer below did the job and it's nice to know that it's a CTP5 bug that's behind this.  EF also needs the cascade behaviour to be specified and I've used the fluent API to do this following the example in the link given by druttka.  Some more good reading from Morteza Manavi <a href="http://weblogs.asp.net/manavi/archive/2011/01/23/associations-in-ef-code-first-ctp5-part-3-one-to-one-foreign-key-associations.aspx" rel="nofollow noreferrer">here</a>.</p>
  
  <p>The code now is this:</p>
  
  <pre><code>public class CustomerContext : DbContext
  {
     public DbSet&lt;Organisation&gt; Organisations { get; set; }
     public DbSet&lt;Person&gt; Persons { get; set; }
  
     protected override void OnModelCreating(ModelBuilder builder)
     {
        DbDatabase.SetInitializer(new DropCreateDatabaseAlways&lt;CustomerContext&gt;());
  
        builder.Entity&lt;Organisation&gt;()
           .HasRequired(p =&gt; p.MainContact)
           .WithMany()
           .HasForeignKey(p =&gt; p.MainContactId)
           .WillCascadeOnDelete(false);
        builder.Entity&lt;Organisation&gt;()
           .Property(p =&gt; p.MainContactId)
           .HasColumnName("MainContact");
  
        builder.Entity&lt;Organisation&gt;()
           .HasRequired(p =&gt; p.FinancialContact)
           .WithMany()
           .HasForeignKey(p =&gt; p.FinancialContactId)
           .WillCascadeOnDelete(false);
        builder.Entity&lt;Organisation&gt;()
           .Property(p =&gt; p.FinancialContactId)
           .HasColumnName("FinancialContact");
     }
  }
  
  public abstract class Customer
  {
     public int Id { get; set; }
     public string Name { get; set; }
  }
  
  public class Person : Customer
  {
     public string Email { get; set; }
  }
  
  public class Organisation : Customer
  {
     public Person FinancialContact { get; set; }
     public int FinancialContactId { get; set; }
  
     public Person MainContact { get; set; }
     public int MainContactId { get; set; }
  }
  </code></pre>
  
  <p>Which now gives the far more suitable database:
  <img src="https://i.stack.imgur.com/GiImC.png" alt="enter image description here"></p>