我有3个表分别是Violation、Comment和自动生成的AspNetUsers。它们之间的关系如下。
我使用代码优先的方法,我的模型如下。为了简洁起见,删除了一些属性。
违规模型
public class Violation
{
public Violation()
{
this.Comments = new HashSet<Comment>();
}
public int Id { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
public virtual ApplicationUser CreatorUser { get; set; }
}
注释模型
public class Comment
{
public int Id { get; set; }
[Required]
public string Content { get; set; }
[Required]
public DateTime PostedDateTime { get; set; }
public ApplicationUser ApplicationUser { get; set; }
public Violation Violation { get; set; }
}
ApplicationUser(AspNetUsers表)
public class ApplicationUser : IdentityUser
{
public ApplicationUser()
{
this.Comments = new List<Comment>();
this.Violations = new List<Violation>();
}
public virtual List<Comment> Comments { get; set; }
public virtual List<Violation> Violations { get; set; }
}
问题是,当我尝试检索Comment的ApplicationUser导航属性时,我看到其中许多属性指向空属性,即使数据库中每个属性都有正确的记录。
很快,EF无法正确检索数据库记录。我坚持了下来,找不到原因。