开发者

Linq to Entities - Endless entity loop

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 thi开发者_运维技巧s 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.


Linq-to-anything never resolves their queries until they have to.

If you look at the variables in the debugger, you will see "Click to expand" on any of the child collections. It's only when you click on these that the database queries will actually be run.

The only way you would load the database into the debugger is if you sit there finding every collection and manually expanding them in the watch window.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜