开发者

Child records from relation don't load (EF4.0, POCO)

开发者 https://www.devze.com 2023-01-12 14:13 出处:网络
I have 2 classes: User and Booklink public class User { public int UserID { get; set; } publi开发者_运维知识库c string Email { get; set; }

I have 2 classes: User and Booklink

        public class User
        {
            public int UserID { get; set; }
            publi开发者_运维知识库c 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; }

        }

I used new EF4.0 feature - POCO.

The problem: when I load user entity BookLinks property is NULL (there are some child records in Booklinks table). But other fields(properties) was loaded fine and all of them (except Booklinks) have their values from database.

I receive objects from generic repository like this:

UsersRepository usersRepository = new UsersRepository();

            User user = usersRepository.FindByID(1);

FindByID method is implemented like this

private ObjectSet<T> _entitySet;    
return _entitySet.AsQueryable().Where(predicate).SingeOrDefault();

And navigation property Booklinks in user instance is NULL

I cant understand why I see this behaivour. How I can load child records automatically?

Here is screenshot from EF designer http://tinyurl.com/2ct45d5 (if it will help...)


Lazy loading can't work on a POCO unless you declare your association virtual (you haven't) and enable proxy creation (the default, IIRC).

0

精彩评论

暂无评论...
验证码 换一张
取 消