title: EF Code First 5 - How to define nullable foreign key using Fluent API?
body: <p>I have two table -</p>
  
  <pre><code> 1. Account 
  
   2. Users
  </code></pre>
  
  <p>In <code>Account</code> table, <code>DefaultExpensePartner</code> and <code>AccountOwner</code> are foreign key of <code>UserId</code> field of <code>Users</code> table. I have defined classes as below.</p>
  
  <pre><code>public class Account
  {
      public int AccountId { get; set; }
      public string AccountName { get; set; }
      public int? AccountOwnerId { get; set; }
      public int? DefaultExpensePartnerId { get; set; }
  
      public virtual Users AccountOwner { get; set; }
      public virtual Users DefaultExpensePartner { get; set; }
  }
  
  public class AccountConfiguration : EntityTypeConfiguration&lt;Account&gt;
  {
      public AccountConfiguration()
      {
          this.ToTable("Account");
  
          this.HasKey(c =&gt; c.AccountId);
  
          this.Property(c =&gt; c.AccountId)
              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
              .IsRequired();
  
          this.Property(c =&gt; c.AccountName)
              .HasMaxLength(50)
              .IsRequired();
  
          this.Property(c =&gt; c.AccountOwnerId)
              .HasColumnName("AccountOwner")
              .IsOptional();
  
          this.Property(c =&gt; c.DefaultExpensePartnerId)
              .HasColumnName("DefaultExpensePartner")
              .IsOptional();
  
          this.HasRequired(c =&gt; c.DefaultExpensePartner)
              .WithMany()
              .HasForeignKey(c =&gt; c.DefaultExpensePartnerId)
              .WillCascadeOnDelete(false);
  
          this.HasRequired(c =&gt; c.AccountOwner)
             .WithMany()
             .HasForeignKey(c =&gt; c.AccountOwnerId)
             .WillCascadeOnDelete(false);
      }
  }
  
  public class Users
  {
      public int UserId { get; set; }
      public string Email { get; set; }
      public string DisplayName { get; set; }
      public string PasswordSalt1 { get; set; }
      public string PasswordSalt2 { get; set; }
      public string PasswordHash { get; set; }
  }
  
  public class UsersConfiguration : EntityTypeConfiguration&lt;Users&gt;
  {
      public UsersConfiguration()
      {
          this.ToTable("Users");
  
          this.HasKey(c =&gt; c.UserId);
  
          this.Property(c =&gt; c.UserId)
              .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
              .IsRequired();
  
          this.Property(c =&gt; c.Email)
              .HasMaxLength(80)
              .IsRequired();
  
          this.Property(c =&gt; c.DisplayName)
              .HasMaxLength(50)
              .IsRequired();
  
          this.Property(c =&gt; c.PasswordSalt1)
              .HasMaxLength(172)
              .IsRequired();
  
          this.Property(c =&gt; c.PasswordSalt2)
              .HasMaxLength(172)
              .IsRequired();
  
          this.Property(c =&gt; c.PasswordHash)
              .HasMaxLength(40)
              .IsRequired();
      }
  }
  </code></pre>
  
  <p>Though I am able to establish foreign key relationship for <code>AccountOwner</code> and <code>DefaultExpensePartner</code> fields, they are defined as non-null in database which is incorrect according to my original plan. Can anyone know how to define foreign key as nullable?</p>