如何阻止自动生成的 Linq to SQL 类加载所有数据?
重复的如何阻止自动生成的 Linq to SQL 类加载所有数据? 在那里发布答案!
我有一个 ASP.NET MVC 项目,很像 NerdDinner 教程示例。 (我使用的是 MVC 2,但遵循 NerdDinner 教程来创建它)。
根据本教程的第 3 部分中的说明,我创建了一个 Linq-我的数据库的 to-SQL 模型,通过创建“Linq to SQL 类”(.dbml) 表面,并将我的数据库表拖放到其上。设计器根据我的数据库表自动添加了生成的类之间的关系。
假设我的类按照 NerdDinner 示例,所以我有晚餐和 RSVP 表,其中每个晚餐记录都与许多 RSVP 记录相关联 - 因此在生成的类中,晚餐对象有一个 RSVPs 属性,它是 RSVP 列表对象。
我的问题是这样的:它似乎(并且我很高兴被证明是错误的)一旦我 访问晚餐对象,它会加载所有相应的 RSVP 对象,即使我不使用 RSVPs 成员。
第一个问题:这真的是生成的类的默认行为吗?
在我的特殊情况下,对象图包含更多的表(其中有更多数量级的记录),因此这是灾难性的行为 - 当我想做的只是显示某个对象的详细信息时,我会加载大量数据。单亲记录。
第二个问题:是否有通过设计器 UI 公开的任何属性可以让我修改此行为? (我找不到任何)。
第三个问题:我看到了如何通过使用与关联的 DataShape
对象来控制 DataContext
中相关记录的加载的描述。数据上下文。这就是我想要做的吗?如果是的话,有没有像 NerdDinner 那样的教程,不仅可以展示如何做到这一点,而且还可以建议正常使用的“模式”?
DUPLICATE of How can I stop an auto-generated Linq to SQL class from loading ALL data?
post answers there!
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 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于dinner类,它不会自动加载外键关系属性。您必须设置数据加载选项才能使其工作,如下所示:
但是,每当您访问 RSVP 时,默认情况下它都会自动加载这些选项(如果您也导航到智能感知中的属性,仅供参考)。
HTH。
Given the Dinner class, it doesn't automatically load foreign key relationship properties. You have to setup data load options for that to work, as in:
However, anytime you access RSVPs, it will automatically load these by default (and if you navigate to the property in intellisense too, FYI).
HTH.