ObjectTrackingEnabled和LINQ到SQL [英] ObjectTrackingEnabled and linq-to-sql

查看:214
本文介绍了ObjectTrackingEnabled和LINQ到SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读的这里,对于读操作查询数据时,设置 ObjectTrackingEnabled 给有些性能提升的。我的查询是这样的:

I read here that when querying data for a read operation, setting ObjectTrackingEnabled to false gives somewhat of a performance boost. My queries look like this:

public return type TheQueryName (some parameters)
{
   using (TheDC MyDC = new TheDC())
   {
       var TheQuery = (...).ToList();

       return TheQuery;
   }
}



1)如果我要添加的性能增强,我只是添加行 ObjectTrackingEnabled = TRUE; 只是行前面没有var TheQuery =(...)了ToList ();

1) If I want to add the performance enhancement, do I just add the line ObjectTrackingEnabled = true; just before the line var TheQuery = (...).ToList();

2)同时,如果设置了 ObjectTrackingEnabled 真正在查询中,我需要返回前将其设置为false,还是我只是设置 ObjectTrackingEnabled 对于数据上下文的具体实例,下面的时间我会实例化一个新的数据上下文 ObjectTrackingEnabled 将恢复到<$ C $其默认状态?C>假

2) Also, if I set ObjectTrackingEnabled to true in a query, do I need to set it to false before returning or am I just setting ObjectTrackingEnabled for the particular instance of the data context and the following time I'll instantiate a new data context the value of ObjectTrackingEnabled will revert back to its default state of false?

注意:我只计划做补充 ObjectTrackingEnabled = FALSE; 读取操作。

Note: I only plan do add ObjectTrackingEnabled = false; to read operations.

感谢。

推荐答案

ObjectTrackingEnabled控制是否你的数据上下文( TheDC 你的情况),将它们载入后跟踪更改的实体。无论你想要的或不取决于你的特定情况。

ObjectTrackingEnabled controls whether your Data Context (TheDC in your case) will track changes to the entities after they're loaded. Whether you want that or not depends on your specific case.

当然,具有数据上下文少做事情会让它走得更快,但负担的牺牲你的具有跟踪变化。为什么更改跟踪很酷?因为你可以:

Naturally, having the Data context do fewer things will make it go faster, but at the expense of burdening you with tracking changes. Why is change tracking cool? Because you can:


  1. 从某些方面装载了一些实体

  2. 修改它们

  3. 添加新的,删除其他

  4. 呼叫的SaveChanges 键,让数据的上下文找出该怎么UPDATE查询改性实体,删除删除的,等等

  1. Load up some entities from some context
  2. Modify them
  3. Add new ones, delete others
  4. Call SaveChanges and let the data context figure out to do UPDATE queries for modified entities, DELETE for deleted ones, etc

通过更改跟踪禁用,你必须明确地告诉上下文是什么改变了,什么新值等。

With change tracking disabled, you have to tell the context explicitly what changed, what the new values are, etc.

假设你还是不想目标跟踪,该属性设置在每个特定的数据上下文实例。这意味着你必须要么:

Assuming you still don't want object tracking, the property is set on each specific data context instance. This means you have to either:


  1. 手动设置为您创建的每一个数据上下文

  2. 设置它作为像这样的背景下的构造背景下默认: this.Configuration.AutoDetectChangesEnabled = TRUE; 见的this博客帖子详情

  1. Set it manually for every data context you create
  2. Set it as a default for the context in the context's constructor like so: this.Configuration.AutoDetectChangesEnabled = true; See this blog post for details

希望这有助于!

这篇关于ObjectTrackingEnabled和LINQ到SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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