开发者

LINQ to SQL - Association lost when I turn ObjectTrackingEnabled off

I am using Linq-to-SQL (C# 3.5) to read two tables, house and county, from my database (SQL Server 2000). House has a CountyID column that matches an ID column in the county table. The County table has ID and开发者_Python百科 CountyName columns.There is no association in the database - I added it in the dbml file. (Parent class = County, Child class = House)

Now I can query the House table and get the county name as house.County.CountyName - just what I want.

This works great unless I set EnableObjectTracking false. I get approx 3X performance improvement but the County child object in House is null.

Is there a way around this?


When you set EnableObjectTracking to false, you also turn off lazy loading child entities as you have noticed. MSDN says...

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 can use the LoadWith method to populate your child entities if you want to keep Object Tracking Disabled. Please see MSDN for more information on the LoadWith

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜