EF Code First 5 - How to define nullable foreign key using Fluent API?

Parth Patel picture Parth Patel · Jan 16, 2013 · Viewed 16.7k times · Source

I have two table -

 1. Account 

 2. Users

In Account table, DefaultExpensePartner and AccountOwner are foreign key of UserId field of Users table. I have defined classes as below.

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<Account>
{
    public AccountConfiguration()
    {
        this.ToTable("Account");

        this.HasKey(c => c.AccountId);

        this.Property(c => c.AccountId)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
            .IsRequired();

        this.Property(c => c.AccountName)
            .HasMaxLength(50)
            .IsRequired();

        this.Property(c => c.AccountOwnerId)
            .HasColumnName("AccountOwner")
            .IsOptional();

        this.Property(c => c.DefaultExpensePartnerId)
            .HasColumnName("DefaultExpensePartner")
            .IsOptional();

        this.HasRequired(c => c.DefaultExpensePartner)
            .WithMany()
            .HasForeignKey(c => c.DefaultExpensePartnerId)
            .WillCascadeOnDelete(false);

        this.HasRequired(c => c.AccountOwner)
           .WithMany()
           .HasForeignKey(c => 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<Users>
{
    public UsersConfiguration()
    {
        this.ToTable("Users");

        this.HasKey(c => c.UserId);

        this.Property(c => c.UserId)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
            .IsRequired();

        this.Property(c => c.Email)
            .HasMaxLength(80)
            .IsRequired();

        this.Property(c => c.DisplayName)
            .HasMaxLength(50)
            .IsRequired();

        this.Property(c => c.PasswordSalt1)
            .HasMaxLength(172)
            .IsRequired();

        this.Property(c => c.PasswordSalt2)
            .HasMaxLength(172)
            .IsRequired();

        this.Property(c => c.PasswordHash)
            .HasMaxLength(40)
            .IsRequired();
    }
}

Though I am able to establish foreign key relationship for AccountOwner and DefaultExpensePartner 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?

Answer

Dennis picture Dennis · Jan 16, 2013

If you want to have nullable foreign key, why are you defining it as required:

this.HasRequired

?

Use HasOptional instead:

        this.HasOptional(c => c.AccountOwner)
            .WithMany()
            .HasForeignKey(c => c.AccountOwnerId)
            .WillCascadeOnDelete(false);