开发者

How can I stop an auto-generated Linq to SQL class from loading ALL data?

开发者 https://www.devze.com 2022-12-22 14:38 出处:网络
UPDATE: from what I\'m hearing, I was imagining the problem I describe below. So, this is pretty much a non-question. Future readers, move on... nothing to see here.

UPDATE: from what I'm hearing, I was imagining the problem I describe below. So, this is pretty much a non-question. Future readers, move on... nothing to see here.


I have an ASP.NET MVC project, much like the NerdDinner tutorial example. (I'm using MVC 2, but followed the NerdDinner tutorial in order to create it).

As per the instructions in part 3 of the tutorial, I've created a Linq-to-SQL model of my database by creating a "Linq to SQL Classes" (.dbml) surface, and dropping my database tables onto it. The designer has automatically added relationships between the generated classes based on my database tables.

Let's say that my classes are as per the NerdDinner example, so I have Dinner and RSVP tables, where each Dinner record is associated with many RSVP records - hence in the generated classes, the Dinner object has a RSVPs property which is a list of RSVP objects.

My problem is this: it appears (and I'd be gladly proved wrong on this) that as soon as I access a Dinner object, it's loading all of the corresponding RSVP objects, even if I don't use the RSVPs member.

First question: is this really the default behavior for the generated classes?

In my particular situation, the object graph contains many more tables (which have an order of magnitude more records), and so this is disastrous behaviour - I'd be loading tons of data when all I want to do is show the details of a single parent record.

Second question: are there any properties exposed through the designer UI that would let me modify this behavior? (I can't find any).

Third question: I've seen a description of how to control the loadin开发者_JAVA百科g of related records in a DataContext by using a DataShape object associated with the DataContext. Is that what I'm meant to do, and if so are there any tutorials like the NerdDinner one that would show not only how to do it, but also suggest a 'pattern' for normal use?


When you have a 1:M relationship, the L2S code generator will generate an entity class for the single entity and include an entity set of child objects within the entity. When you retrieve the single (parent) entity, L2S will not retrieve the child objects until you attempt to access the collection of child objects in some way. This is called Lazy Loading. There is no way, that I am aware of, to prevent L2S from retrieving the child objects, other than never attempting to access the collection of child objects. But note, if you never access the child collection, L2S WILL NOT retrieve the child rows. So, since your application is retrieving the child rows, you must be accessing the collection in some way.

You can also perform what is called 'eager loading' by having L2S automatically retrieve the child objects before you attempt to access the collection. However, it doesn't sound like this is what you want to do.


http://www.davidhayden.com/blog/dave/archive/2007/08/05/LINQToSQLLazyLoadingPropertiesSpecifyingPreFetchWhenNeededPerformance.aspx

http://www.davidhayden.com/blog/dave/archive/2009/02/12/LINQToSQLTutorialsLazyLoadingPropertiesPerformance.aspx

0

精彩评论

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

关注公众号