LINQ to SQL: Setting ObjectTrackingEnabled to false breaks lazy loading of child entities?

前端 未结 1 1336
不知归路
不知归路 2021-02-04 01:51

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

I\'m using LINQ2SQL in a set of integration tests. I\'

1条回答
  •  再見小時候
    2021-02-04 02:17

    According to MSDN:

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

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

    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.

    Both are set to true. This is the default.

    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.

    0 讨论(0)
提交回复
热议问题