如何刷新实体框架的上下文 [英] How Can I Refresh My Context Of Entity Framework

查看:106
本文介绍了如何刷新实体框架的上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个小程序(多用户)。



当我运行我的程序的2个会话,在会话1中我更新数据,我无法在会话2中看到更新的数据。但是更新的数据正确存储在数据库中。



当我在会话1中插入时,我在会话2中看到插入。



我的计划是刷新上下文,但这似乎不太可能。



有没有人有同样的问题和一个简单的解决方案?



非常感谢提前!

Hello everyone,

I have a small program (multi user).

When I run 2 sessions of my program and in session 1 I do an update of the data, I can't see the updated data in session 2. However the updated data is stored correctly in the database.

When I do an insert in session 1, I see the insert in session 2.

My plan was to do a refresh of the context, but this doesn't seems to be possible.

Has anyone the same problem and a simple solution?

Many thanks in advance!

推荐答案

您可以使用以下方式刷新所需的数据集:

You could refresh only the needed data set by using:
this.MyObjectContext.Refresh(RefreshMode.StoreWins, this.MyObjectContext.MyObjectSet);<br />



但我建议使用Solution1中的建议并使用上面的方法只有当你真正需要它时!


But I recommend to use the advises from Solution1 and to use the method above only when you realy need it!


如果你正在创建一个n上下文的实例并在应用程序的生命周期中保持活动状态,你做错了。



当你需要进行查询时创建一个上下文完成后处理它。这样,您就不必刷新它。当你对它进行下一次查询时,它会拾取新数据。
If you're creating an instance of the context and keeping it alive for the lifetime of the application, you're doing it wrong.

Create a context when you need to do a query and Dispose it when you're done. This way, you don't have to "refresh" it. It'll pickup the new data when ever you do your next query for it.


你可以使用上下文的Refresh方法或处理旧的上下文并重新创建。但是,如果只需要在上下文更改时进行刷新,则只要数据库发生更改就需要自动刷新。然后你可以尝试下面的SQL Server Service Broker文章。

使用SQL Server Service Broker自动刷新实体框架数据 [ ^ ]
you can use Refresh method of the context or dispose the old context and create again. But if you need to refresh only when the context change and it need to be happen automatically whenever database changed. Then you can try below article with SQL Server Service Broker.
AutoRefresh Entity Framework Data Using SQL Server Service Broker[^]


这篇关于如何刷新实体框架的上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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