C# 关系don'中的子记录;t荷载(EF4.0,POCO)

C# 关系don'中的子记录;t荷载(EF4.0,POCO),c#,entity-framework-4,C#,Entity Framework 4,我有两门课:User和Booklink public class User { public int UserID { get; set; } public string Email { get; set; } public string Login { get; set; } public string Surname { get; set; }

我有两门课:UserBooklink

        public class User
        {
            public int UserID { get; set; }
            public string Email { get; set; }
            public string Login { get; set; }
            public string Surname { get; set; }
            public string Name { get; set; }
            public int Points { get; set; }
            public string Password { get; set; }

            public IEnumerable<BookLink> BookLinks { get; set; }
        }

        public class BookLink
        {
            public int LinkID { get; set; }
            public int BookID { get; set; }
            public int UserID { get; set; }
            public DateTime EventDate { get; set; }

            public Book Book { get; set; }
            public User User { get; set; }

        }
FindByID方法是这样实现的

private ObjectSet<T> _entitySet;    
return _entitySet.AsQueryable().Where(predicate).SingeOrDefault();
private ObjectSet\u entitySet;
返回_entitySet.AsQueryable().Where(谓词).singerdefault();
用户实例中的导航属性Booklinks为空

我不明白为什么我会看到这种行为。如何自动加载子记录


这里是EF designer的屏幕截图(如果有帮助的话…

延迟加载无法在POCO上工作,除非您声明您的关联
虚拟(您没有)并启用代理创建(默认值,IIRC)。

我设置了ContextOptions.LazyLoadingEnabled=true,但我的问题没有解决:-(我设置了“ContextOptions.ProxyCreationEnabled=true;”并将BookLinks集合标记为虚拟-“公共虚拟IEnumerable BookLinks{get;set;}“但是我的问题没有解决。Booklinks属性仍然为空。您还必须启用LazyLoading。这是ContextOptions上的选项之一。我通过以下方式解决了我的问题:公共虚拟ICollection Booklinks{get;set;}但我不明白IEnumerable和ICollection之间有巨大的差异吗?这是有意义的。您认为?”(也许更重要的是,EF)不能添加到IEnumerable。
private ObjectSet<T> _entitySet;    
return _entitySet.AsQueryable().Where(predicate).SingeOrDefault();