我可以有一个动态更新的 dataContext 吗?
我 90% 的数据访问都使用 DataContext。但是,例如,如果 User1 修改记录并且 User2 查询 DataContext,他将看不到这些修改。因此,每次访问数据时(在每次使用 LINQ to SQL 之前),我都会重新创建 DataContext。
一定有更好的方法来查询表!要么我必须让 DataContext 与表同步,要么我必须找到一种直接查询表的方法。
任何帮助将不胜感激!
谢谢你!
I use DataContexts for 90% of my data access. But if, for instance, User1 modifies a record and User2 query the DataContext, he won't see the modifications. So I recreate my DataContext EVERY time I acces data (before every LINQ to SQL use).
There must be a better way to query the tables! Either I have to get DataContexts to be synchronized with the tables, either I have to find a way to directly query the tables.
Any help would be appreciated!
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,没有更好的方法,也没有必要。
创建 DataContext 相对便宜,唯一昂贵的部分是 Connection,它由 ConnectionPool 处理。
因此,您可以根据查询(它们是真正的费用)和替代方案(缓存结果集)来思考和推理。
No, there is no better way and there is no need for one.
Creating a DataContext is relatively cheap, the only expensive part is the Connection and that is handled by the ConnectionPool.
So you can just think and reason in terms of Queries (they are the real expense) and the alternative, caching resultsets.
您可以按照以下博客文章清除 Linq2Sql 中的缓存:
http://blog.robustsoftware.co.uk/2008/11/clearing-cache-of-linq-to-sql.html
作者还为我们将代码放在了一个扩展方法中:)
注意:您可能需要更改方法签名中的上下文。完成此操作后,您可以调用 db.ClearCache();一切都会很美好……tm。
我认为还有另一种获取数据的方法,但不记得在哪里找到了这些信息,所以可能需要一段时间,如果我找到它,我会对此答案发表评论。
斯图
You can clear the cache in Linq2Sql as per the following blog post:
http://blog.robustsoftware.co.uk/2008/11/clearing-cache-of-linq-to-sql.html
The author also puts the code inside an extension method for us :)
Note: you will probably need to change the Context in the method signature. Once this has been done you can call db.ClearCache(); and it will all be lovely... tm.
I think there is another way of getting the data but can't remember where I found the information so may take me a while, will comment on this answer if i find it.
hth,
Stu
我相信您可能误解了 DataContext 的本质。
在大多数情况下,您需要使用Using 语句创建DataContext 的实例。然后,您应该在 using 语句中继续为 CRUD 操作执行 LINQ 查询。
保存上面的 DataContext 之后打开的任何其他 DataContext 都将看到对 SomeColumn 所做的更改。
I believe that you may misunderstand the nature of the DataContext.
In most situtions, you'll want to create an instance of the DataContext with the Using statement. Within the Using statement you should then proceed to do your LINQ queries for your CRUD operations.
Any other DataContext that is opened after the DataContext above has been saved will see the changes that were made to SomeColumn.