Derik Whittaker

Syndication

News


EntityCollection or EntityReferece error in EF when using .ToList()

This is just odd and I am hoping someone can explain it to me.  Today when doing a EF query (see below) I received the following error.

image

Here is the original EF Query which was causing the error

image

At first I was a bit stumped because this same query worked inside of LINQPad, which I know is using Linq-to-sql not Linq-to-Entities but they are close enough.

After a bit of thinking and staring at the code I thought maybe it was the compiler warning about access to disposed closures when accessing the ‘db’ variable, but that was not the issue either. 

For grins I decided to remove the .ToList() from my statement (making it look like below) and it simply worked.

image

What I don’t understand really is WHY?  Does anyone out there know the reason why?

Till next time,


Posted 05-18-2012 6:38 AM by Derik Whittaker
Filed under:

[Advertisement]

Comments

Peter Ritchie wrote re: EntityCollection or EntityReferece error in EF when using .ToList()
on 05-18-2012 8:02 AM

What "worked"?  Removing ToList with that code alone means nothing will happen.  ToList forces the enumerable to be enumerated...

Richard Dingwall wrote re: EntityCollection or EntityReferece error in EF when using .ToList()
on 05-18-2012 9:05 AM

So it worked when you deferred the enumeration till later? What else was going on at that point?

Alternatively, try breaking the query up into steps and call .ToList() on each step until it breaks.

Lars Kemmann wrote re: EntityCollection or EntityReferece error in EF when using .ToList()
on 05-18-2012 10:24 AM

I've run into similar errors a number of times - it seems to happen whenever I send EF a query expression that's too complex for it to process.  Like Peter said, ToList() forces the enumeration (in this case, it forces EF to attempt to create and execute its query).  If you can, break the query up into smaller pieces that you pre-evaluate with ToList() and then use Linq-to-objects to handle the rest.  

Anon wrote re: EntityCollection or EntityReferece error in EF when using .ToList()
on 05-18-2012 1:10 PM

Are you passing objects between 2 different contexts?

Mike Brown wrote re: EntityCollection or EntityReferece error in EF when using .ToList()
on 06-30-2012 12:31 PM

Do you have navigation properties on these objects? (E.g. ScheduledEvent.EmailSendRequests). You should use the Navigation properties on your entities in your query rather than constructing the join manually as you are doing.

Other than that I can't see anything wrong per se with the code that should generate that error.

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)