NULL values in object properties in Entity Framework
Tables: Article
, Author
, Comment
(1 article and 1 author can have * comments)
In the database is 1 article, 1 author and 1 comment.
The problem is, that code
myBD my_bd = new myBD();
var articles = by_bd.Article;
works OK, I can see that an Author
and an Article
has 1 comment.
But that code
var comm = (from u in my_bd.C开发者_开发问答omment
where ......
select u);
returns the comment but it has NULL values in property Article
and Author
. Why ?
Entity Framework does not support Lazy Loading (yet) and is pessimistic by default. In order to get linked object as collections you have to include them in your query explicitly.
var comm = from u in my_bd.Comment.Include("Article").Include("Author")
where ......
select u;
By doing this you are explicitly telling EF to do the joins when it creates the query. Now you should be able to select those properties.
An alternative to using .Include in your initial query, you can use explicit loading using the .Load() method, e.g. myCustomer.OrdersReference.Load()
There are apparently differences in how many sql queries are executed and how much data is transmitted.
Explained well here: http://www.robbagby.com/entity-framework/is-lazy-loading-in-ef-4-evil-or-the-second-coming/
精彩评论