如何清除LINQ的DataContext的缓存到SQL

问题描述:

,我只使用LINQ从数据库中读取数据,我可以把通过其他方式更改。 (这个不能改变,这是我们正在扩大,所有更新必须走线槽其SDK的应用程序的限制)。

I'm using Linq to Sql to query some database, i only use Linq to read data from the DB, and i make changes to it by other means. (This cannot be changed, this is a restriction from the App that we are extending, all updates must go trough its sdk).

这是好的,但我打一些缓存的问题,基本上,我查询使用LINQ一排,然后我删除它槽外部手段,然后我在外部创建一个新的行,如果我查询该行再次使用LINQ我得到了旧(缓存)数据。

This is fine, but I'm hitting some cache problems, basically, i query a row using Linq, then i delete it trough external means, and then i create a new row externally if i query that row again using linq i got the old (cached) data.

由于这似乎阻止自动加载相关propertys数据上下文(外键),我无法关闭对象跟踪。

I cannot turn off Object Tracking because that seems to prevent the data context from auto loading associated propertys (Foreign Keys).

有什么办法来清除缓存DataContex?

Is there any way to clear the DataContex cache?

我发现sufring网的方法,但它似乎并不安全:的http://blog.robustsoftware.co.uk/2008/11/clearing-cache-of-linq-to-sql.html

I found a method sufring the net but it doesn't seem safe: http://blog.robustsoftware.co.uk/2008/11/clearing-cache-of-linq-to-sql.html

你怎么认为呢? ?我有哪些选择

What do you think? what are my options?.

如果要刷新一个特定的对象,那么的刷新可能是你最好的选择()方法。

If you want to refresh a specific object, then the Refresh() method may be your best bet.

这样的:

Context.Refresh(RefreshMode.OverwriteCurrentValues, objectToRefresh);

您还可以通过对象的数组或一个IEnumerable作为第二个参数,如果你需要刷新更多比一次一个对象。

You can also pass an array of objects or an IEnumerable as the 2nd argument if you need to refresh more than one object at a time.

我看到你在评论中说些什么,在反射器可以看到里面.REFRESH()这种情况发生:

I see what you're talking about in comments, in reflector you see this happening inside .Refresh():

object objectByKey = context.Services.GetObjectByKey(trackedObject.Type, keyValues);
if (objectByKey == null)
{
    throw Error.RefreshOfDeletedObject();
}

您链接的方法似乎是您最好的选择,DataContext类没有按' ŧ提供任何其他的方式来清除已删除的行。处置检查,如在 ClearCache()方法内......这真的只是检查处理,并要求 ResetServices() CommonDataServices underneath..the只有不良效果将清除任何未决的插入,更新或删除您已经排队。

The method you linked seems to be your best option, the DataContext class doesn't provide any other way to clear a deleted row. The disposal checks and such are inside the ClearCache() method...it's really just checking for disposal and calling ResetServices() on the CommonDataServices underneath..the only ill-effect would be clearing any pending inserts, updates or deletes that you have queued.

还有一个选项,你可以火起来的另一个DataContext的为你做什么操作?它不会有任何缓存,它...但也不涉及一些计算成本,所以如果挂起的插入,更新和删除都没有问题,我会用 ClearCache坚持() 方法。

There is one more option, can you fire up another DataContext for whatever operation you're doing? It wouldn't have any cache to it...but that does involve some computational cost, so if the pending insert, update and deletes aren't an issue, I'd stick with the ClearCache() approach.