EF Code First 5 - 如何使用Fluent API定义可为空的外键?

Par*_*tel 5 c# fluent entity-framework-5

我有两张桌子 -

 1. Account 

 2. Users
Run Code Online (Sandbox Code Playgroud)

Account表中,DefaultExpensePartnerAccountOwner有外键UserId的字段Users的表.我已经定义了类如下.

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

虽然我能够为字段AccountOwnerDefaultExpensePartner字段建立外键关系,但是根据我的原始计划,它们在数据库中被定义为非空,这是不正确的.谁能知道如何将外键定义为可空?

Den*_*nis 16

如果您想拥有可以为空的外键,为什么要根据需要定义它:

this.HasRequired
Run Code Online (Sandbox Code Playgroud)

HasOptional改为使用:

        this.HasOptional(c => c.AccountOwner)
            .WithMany()
            .HasForeignKey(c => c.AccountOwnerId)
            .WillCascadeOnDelete(false);
Run Code Online (Sandbox Code Playgroud)


fli*_*erg 3

除非您确实想要,否则您不必指定关键属性,这有点像代码优先的想法:)

根据Microsoft 的这篇文章,删除外键属性应该就足够了。看看文章中图1和图2的区别。(密钥当然会在数据库级别生成,但是您真的需要在模型中使用它们吗?)