goombaloon
goombaloon

Reputation: 3099

Many To Many Query In EF Keeps Returnning Empty Results

I'm using EF 4 with POCO and lazy loading and proxy creation disabled (due to serialization requirements). I have two tables that have a many to many relationship using a third cross-reference table. In EF, the cross-reference entity does not exist and I have one to many navigation properties on both sides of the entity relationships (as expected).

However, when I use the naviation property of one of the entities that I have confirmed via SQL Manager queries to have data, I get an empty result set.

Has anyone else run into this? If so, any advice on how to resolve would be most appreciated.

Thanks!

Upvotes: 1

Views: 226

Answers (2)

indiPy
indiPy

Reputation: 8062

You need to use LoadProperty or Include method.

           Order order = EFContext.Orders.Include("Lines")
                         .Where(m => m.OrderID == orderId).First();

More from [MSDN] "Because POCO entities do not have the same relationship requirements as objects that inherit from EntityObject, a slightly different process is required to load related objects"

Upvotes: 1

CodingGorilla
CodingGorilla

Reputation: 19842

You cannot disable proxy creation and also use lazy loading. (See Is Deferred (Lazy) Loading supported with POCO? @ http://blogs.msdn.com/b/adonet/archive/2009/05/21/poco-in-the-entity-framework-part-1-the-experience.aspx).

The proxies are what make the lazy loading work. That link (above) also demonstrates how to eagerly load the related types, which you'll want to do if you keep proxy creation off.

Upvotes: 1

Related Questions