实体hasoptional foreign keyone to one

时间:2017-03-14 12:26:26

标签: c# entity-framework

我有这堂课:

public class CommunityUser : BaseEntity
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public int CustomerId { get; set; }
    public DateTime CreatedOnUtc { get; set; }
    public int ForumPostsNumber { get; set; }
    public virtual Customer Customer { get; set; }
}

如何使用实体框架映射来表示CommunityUser有一个可选的CustomerId,它是Customer Table上的外键?

1 个答案:

答案 0 :(得分:1)

您必须通过将其设为int?

来使您的外键可为空
public class CommunityUser : BaseEntity
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public int? CustomerId { get; set; }
    public DateTime CreatedOnUtc { get; set; }
    public int ForumPostsNumber { get; set; }
    public virtual Customer Customer { get; set; }
}