如何清除 Linq to Sql 上的 DataContext 缓存 [英] How to clear the DataContext cache on Linq to Sql

查看:36
本文介绍了如何清除 Linq to Sql 上的 DataContext 缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Linq to 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.

我无法关闭对象跟踪,因为这似乎会阻止数据上下文自动加载关联的属性(外键).

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?

我在网上找到了一种方法,但它似乎并不安全: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?.

推荐答案

如果要刷新特定对象,则Refresh() 方法 可能是你最好的选择.

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() 方法内...它实际上只是检查处置并在 CommonDataServices 上调用 ResetServices()在下面..唯一的不良影响是清除您已排队的任何挂起的插入、更新或删除.

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.

这篇关于如何清除 Linq to Sql 上的 DataContext 缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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