title: Specifying Foreign Key Entity Framework Code First, Fluent Api
body: <p>I have a question about defining Foreign Key in EF Code First Fluent API.
  I have a scenario like this:</p>
  
  <p>Two class Person and Car. In my scenario Car can have assign Person or not (one or zero relationship). 
  Code:</p>
  
  <pre><code>    public class Person
  {
      public int Id { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
  }
  
  public class Car
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public Person Person { get; set; }
      public int? PPPPP { get; set; }
  }
  
  class TestContext : DbContext
  {
      public DbSet&lt;Person&gt; Persons { get; set; }
      public DbSet&lt;Car&gt; Cars { get; set; }
  
      public TestContext(string connectionString) : base(connectionString)
      {
      }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;Car&gt;()
                      .HasOptional(x =&gt; x.Person)
                      .WithMany()
                      .HasForeignKey(x =&gt; x.PPPPP)
                      .WillCascadeOnDelete(true);
  
          base.OnModelCreating(modelBuilder);
      }
  }
  </code></pre>
  
  <p>In my sample I want to rename foreign key PersonId to PPPPP. In my mapping I say:</p>
  
  <pre><code>modelBuilder.Entity&lt;Car&gt;()
              .HasOptional(x =&gt; x.Person)
              .WithMany()
              .HasForeignKey(x =&gt; x.PPPPP)
              .WillCascadeOnDelete(true);
  </code></pre>
  
  <p>But my relationship is one to zero and I'm afraid I do mistake using WithMany method, but EF generate database with proper mappings, and everything works well.</p>
  
  <p>Please say if I'm wrong in my Fluent API code or it's good way to do like now is done.</p>
  
  <p>Thanks for help.</p>