At the moment I’m working on a very big database model for one of my websites. I’m using Linq-to-entities for talking to the database. Now whenever I get an object from my context I see this problem:
- Entity “BookReader” got a list with all the books he has read. But entity “Book” also got a list with all the bookreaders that have read the book. Now that would mean that whenever my datacontext returns a bookreader entity in debug mode and I look within the list of books he has read I also see all the readers in there and the books they read. etc etc.
It looks like an endless loop that would that my whole database would be loaded into the memory. Am I making a mistake of thinking this or does this really give a performance problem?
I don’t think so. EF never loads related entities until you or your code instructs it to do that. You mentioned that you see that in debugger. That means that you triggered lazy loading of related entity by opening the parent entity – related collection is empty until you try to access it for the first time and browsing the entity in debugger counts as access. You can check it in SQL profiler.