How do I define Foreign Key Optional Relationships in FluentAPI/Data Annotations with the Entity Framework?

前端 未结 4 2108
轻奢々
轻奢々 2020-12-31 00:16

I have a (sample) application with the following code:

public class Posts
{

    [Key]
    [Required]
    public int ID { get; set; }

    [Required]
    pub         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-31 01:00

    The reason it wasn't allowing nulls because the following:

    public int PollID { get; set; }
    public virtual Poll Poll { get; set; }
    
    public int PostID { get; set; }
    public virtual Post Post { get; set; }
    

    should have been

    public int? PollID { get; set; }
    public virtual Poll Poll { get; set; }
    
    public int? PostID { get; set; }
    public virtual Post Post { get; set; }
    

提交回复
热议问题