我有三个POCO实体类:
public class User {
public int Id { get; set; }
public string UserName { get; set; }
}
public class BlogPost {
public int Id { get; set; }
public string Text { get; set; }
public virtual ICollection<BlogComment> Comments { get; set; }
}
public class BlogComment {
public int Id { get; set; }
public int PostId { get; set;
public int? UserId { get; set; }
public string Text { get; set; }
public virtual BlogPost Post { get; set; }
public virtual User User { get; set; }
}
要强调的几点要点:
在我的Entity Framework配置中,我从BlogComment端定义了BlogPost和BlogComment之间的关系:
HasRequired(t => t.Post)
.WithMany(t => t.Comments)
.HasForeignKey(t => t.PostId);
但我很难理解如何构建对用户的可选引用:
HasOptional(t => t.User)
// What comes next, and why?
任何建议都将受到赞赏。