如何从加载所有数据停止自动生成的LINQ到SQL类? [英] How can I stop an auto-generated Linq to SQL class from loading ALL data?

查看:148
本文介绍了如何从加载所有数据停止自动生成的LINQ到SQL类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:从我的耳朵,我想象我在下面说明问题。所以,这是pretty多非的问题。未来的读者,继续......没什么可看这里。

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.

我有一个ASP.NET MVC项目,很像的NerdDinner 教程示例。 (我使用MVC 2,但跟着教程的NerdDinner为了创建它)。

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).

根据教程的第3部分,我创建了一个LINQ到SQL模型我的数据库创建一个LINQ to SQL类(的.dbml)面,和删除我的数据库表到它的。设计师已经自动添加基于我的数据库表生成的类之间的关系。

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.

让我们说,我的班是按中的NerdDinner例子,所以我有Dinner和RSVP表,其中每个晚餐记录与许多RSVP记录相关联 - 因此在生成的类中,晚餐对象有一个回函属性,该属性是一个RSVP对象列表。

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.

我的问题是这样的:它的出现的(我会欣然证明是错误的这一点),一旦我
访问Dinner对象,它加载所有相应的RSVP对象,即使我不使用回函成员。

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.

第二个问题:是否有经过设计师UI,将让我修改此行为暴露的任何属性? (我找不到任何)。

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

第三个问题:我已经使用<$ C看到了如何控制的相关记录在的DataContext 加载描述$ C>与DataContext的相关DataShape 对象。难道这就是我的意思做,如果是的话有没有喜欢的NerdDinner一个会展现的不仅是如何做到这一点,而且任何教程建议正常使用一个'模式'?

Third question: I've seen a description of how to control the loading 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?

推荐答案

当你有一个1:M的关系,在L2S code发生器产生的单一实体的实体类,并包括子对象的实体集实体内。当您检索单(父)的实体,L2S不会检索子对象,直到您尝试访问子对象的集合中的某种方式。这就是所谓的延迟加载。没有办法,我是知道的,以prevent L2S从检索子对象,总比不尝试访问子对象的集合等。但要注意,如果你从来没有访问子集,L2S将不检索子行。所以,既然您的应用程序检索子行,你必须访问以某种方式集合。

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.

您也可以通过让L2S自动检索您试图访问集合前的子对象进行所谓预先加载。但是,它听起来并不像这是你想要做的事情。

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.

这篇关于如何从加载所有数据停止自动生成的LINQ到SQL类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆