C# 如何清除 Linq to Sql 上的 DataContext 缓存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2098143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to clear the DataContext cache on Linq to Sql
提问by albertein
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 to Sql 查询一些数据库,我只使用 Linq 从数据库读取数据,我通过其他方式对其进行更改。(这是无法更改的,这是我们正在扩展的应用程序的限制,所有更新都必须通过其 sdk)。
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.
这很好,但我遇到了一些缓存问题,基本上,我使用 Linq 查询一行,然后通过外部方式将其删除,然后如果我再次使用 linq 查询该行,我会在外部创建一个新行,我得到了旧的(缓存)数据。
I cannot turn off Object Tracking because that seems to prevent the data context from auto loading associated propertys (Foreign Keys).
我无法关闭对象跟踪,因为这似乎会阻止数据上下文自动加载关联属性(外键)。
Is there any way to clear the DataContex cache?
有没有办法清除DataContex缓存?
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
我在网上找到了一种方法,但它似乎并不安全:http://blog.robustsoftware.co.uk/2008/11/clearing-cache-of-linq-to-sql.html
What do you think? what are my options?.
你怎么认为?我有哪些选择?
采纳答案by Nick Craver
If you want to refresh a specific object, then the Refresh() methodmay be your best bet.
如果你想刷新一个特定的对象,那么Refresh() 方法可能是你最好的选择。
Like this:
像这样:
Context.Refresh(RefreshMode.OverwriteCurrentValues, objectToRefresh);
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.
如果您需要一次刷新多个对象,您还可以传递一组对象或一个 IEnumerable 作为第二个参数。
Update
更新
I see what you're talking about in comments, in reflector you see this happening inside .Refresh():
我在评论中看到你在谈论什么,在反射器中你看到这种情况发生在 .Refresh() 中:
object objectByKey = context.Services.GetObjectByKey(trackedObject.Type, keyValues);
if (objectByKey == null)
{
throw Error.RefreshOfDeletedObject();
}
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()
方法......这真的只是检查处理,并呼吁ResetServices()
对CommonDataServices
underneath..the唯一不良效应会清除已排队的任何未决的插入,更新或删除。
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.
还有一个选择,您可以为正在执行的任何操作启动另一个 DataContext 吗?它不会有任何缓存......但这确实涉及一些计算成本,所以如果挂起的插入、更新和删除不是问题,我会坚持使用这种ClearCache()
方法。
回答by Brian Mains
You should be able to just requery the result sets that are using this objects. This would not pull a cached set, but would actually return the final results. I know that this may not be as easy or feasible depending on how you setup your app...
您应该能够重新查询使用此对象的结果集。这不会拉出缓存集,但实际上会返回最终结果。我知道这可能不那么容易或可行,具体取决于您设置应用程序的方式...
HTH.
哈。
回答by Tiago Gouvêa
I made this code to really CLEAR the "cached" entities, detaching it.
我制作这段代码是为了真正清除“缓存”实体,将其分离。
var entidades = Ctx.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Deleted | EntityState.Modified | EntityState.Unchanged);
foreach (var objectStateEntry in entidades)
Ctx.Detach(objectStateEntry.Entity);
Where Ctx are my Context.
Ctx 是我的上下文。