代码之家  ›  专栏  ›  技术社区  ›  paulpitchford

EF6代码第一MVC相关实体为空

  •  1
  • paulpitchford  · 技术社区  · 12 年前

    我有两门课:

    public class Players
    {
        [Key]
        public int Id { get; set; }
        public string PlayerName { get; set; }
    
        public virtual ICollection<ResultItems> ResultItems { get; set; }
    }
    
    public class ResultItems
    {
        [Key]
        public int Id { get; set; }
        public int ResultsId { get; set; }
        public Results Results { get; set; }
        public int Position { get; set; }
        public int PlayerId { get; set; }
        public Players Player { get; set; }
        public double Cash { get; set; }
        public int Points { get; set; }
    }
    

    您将在这里看到,当我们输入ResultItems时,数据库正在很好地填充:

    screenshot

    然而,当页面加载时,我们循环模型以创建数据表,Player实体为空:

    screenshot

    有人能帮我理解为什么它没有加载吗?任何帮助都将不胜感激。

    谢谢

    保罗。

    1 回复  |  直到 12 年前
        1
  •  2
  •   Moe Bataineh    12 年前

    您需要添加 virtual 关键字添加到您的 public Players Player 或者你可以 db.ResultItems.Include("Player").ToList()