实体框架:如何检测外部更改数据库 [英] Entity Framework: How to detect external changes to database

查看:129
本文介绍了实体框架:如何检测外部更改数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储过程改变大量数据在数据库中。此存储过程是从在同一时间使用EF对于数据运算的应用程序调用。

I have a stored procedure that changes lots of data in the database. This stored procedure is called from the application that at the same time uses EF for data operations.

于是我点击一个按钮,存储过程是在数据库中运行,数据改变和EF显示旧数据给用户。

So I click a button, stored procedure is run at the database, data is changed and EF shows old data to the user.

有没有办法来强制的DbContext或ObjectContext的从数据库刷新数据? ObjectContext.Refresh()可能是解决办法,但我不想要求可能被改变每一个表这种方法。我希望所有的表中的一招被刷新。

Is there a way to force the DbContext or ObjectContext to refresh data from database? ObjectContext.Refresh() may be the solution but I do not want to call this method for every single table that may be changed. I want all the tables to be refreshed in one move.

我使用实体框架5,针对.NET 4.0

I am using Entity Framework 5, targeting .NET 4.0

编辑:添加数据可用,但在现有的数据修改不是由EF体现。我看到新添加的记录,但我看不到我对现有记录所做的更改。

推荐答案

的DbContext 应该是短暂的。创建它,运行查询,并处理它。

Your DbContext should be short-lived. Create it, run your query, and dispose it.

using (var context = new MyProject.DbContext())
{
    // run your query here
}

不要让上下文各地。这样,你不会有旧数据的任何问题。

Don't keep your context around. That way you won't have any issues with old data.

这篇关于实体框架:如何检测外部更改数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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