views:

107

answers:

2

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.Comment
                     where ......
                     select u);

returns the comment but it has NULL values in property Article and Author. Why ?

+1  A: 

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.

Josh
Thank for Your post, now it's working :)btw, do You know some good books/ebooks about Entity Framework ?
Tony
In fact I do happen to know a good E-Book on EF: (http://msmvps.com/blogs/brianmadsen/archive/2009/04/10/free-500-page-pdf-on-the-entity-framework.aspx) It is worth noting however, that you might want to start looking at what is possible with Entity Framework 4 as it will be very different from what you are working with now.
Josh
A: 

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/

Jason Roberts