LINQ to SQL:将ObjectTrackingEnabled设置为false会中断子实体的延迟加载? [英] LINQ to SQL: Setting ObjectTrackingEnabled to false breaks lazy loading of child entities?

查看:122
本文介绍了LINQ to SQL:将ObjectTrackingEnabled设置为false会中断子实体的延迟加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以禁用LINQ的对象跟踪功能,同时又不禁用延迟加载的子关联?

Is there a way to disable LINQ's object tracking features, without also disabling lazy-loaded child associations?

我在一组集成测试中使用LINQ2SQL.我仅使用LINQ来验证数据库中的更改,因此我希望它像一个简单的数据访问层,而不是一个完整的ORM.为此,我将数据上下文的ObjectTrackingEnabled属性设置为false,以防止LINQ缓存数据.除破坏实体之间的关联外,此方法工作正常.

I'm using LINQ2SQL in a set of integration tests. I'm only using LINQ to verify changes in the database, so I want it to act like a simple data access layer rather than a full ORM. To do this,I set set the data context's ObjectTrackingEnabled property to false to prevent LINQ from caching data. This works fine, except that it breaks associations between entities.

例如,假定两个表WIDGET和CATEGORY,它们之间具有FK关系.启用对象跟踪后, widgetInstance.CATEGORY 已正确延迟加载.禁用对象跟踪后,不会延迟加载任何内容,并且CATEGORY属性始终为null.

For instance, assume two tables WIDGET and CATEGORY, with a FK relationship between them. With object tracking enabled, widgetInstance.CATEGORY is correctly lazy-loaded. With object tracking disabled, nothing is lazy loaded and the CATEGORY property is always null.

如何防止LINQ缓存数据,又不阻止延迟加载?

How do I prevent LINQ from caching data, without also preventing lazy-loading?

更新:这是 LoadWith 急于加载子数据.就我而言,我只需要数据,所以渴望加载是可以的.

UPDATE: Here's a link to the MSDN page referenced by the answer. Further research also turned up that I can use LoadWith to do eager loading of child data. In my case I just want the data, so eager loading is fine.

推荐答案

根据MSDN:

延迟加载需要对象 追踪.仅以下三个 模式有效:

Deferred loading requires object tracking. Only the following three modes are valid:

ObjectTrackingEnabled =否. DeferredLoadingEnabled被忽略,​​并且 推断是假的.这种行为 对应于只读 DataContext.

ObjectTrackingEnabled = false. DeferredLoadingEnabled is ignored and inferred to be false. This behavior corresponds to a read-only DataContext.

ObjectTrackingEnabled = true. DeferredLoadingEnabled = false.这 情况对应于一个DataContext 允许用户加载对象 图通过使用LoadWith指令, 但它不会启用延迟 正在加载.

ObjectTrackingEnabled = true. DeferredLoadingEnabled = false. This situation corresponds to a DataContext that allows users to load an object graph by using LoadWith directives, but it does not enable deferred loading.

两者都设置为true.这是 默认.

Both are set to true. This is the default.

您要么必须启用对象跟踪并分离实体(我想这是可行的,因为您正在运行测试,否则分离…的另一个实体的每个单个实体都非常麻烦),或者使用DataLoadOptions.通过LoadWith()或AssociateWith()可以快速加载关系.

You either have to enable object tracking and detach entities (which is doable since you're running tests, I guess, otherwise it pretty much troublesome to detach every single entity of another entity of...), or use DataLoadOptions.LoadWith() or AssociateWith() to eager-load the relationships.

这篇关于LINQ to SQL:将ObjectTrackingEnabled设置为false会中断子实体的延迟加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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