实体框架数据上下文不与数据库同步? [英] Entity framework data context not in sync with database?

查看:154
本文介绍了实体框架数据上下文不与数据库同步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,这里是这样的情况 -

So, here is the situation -


  • 我在调用AddtoObject()的数据库中插入一个项目,然后调用SaveChanges()

  • 然后,我调用一个存储过程来更新当前插入的记录。

  • 然后,我再次调用Save changes() li>
  • 当我查询数据库时,数据库具有正确的更新值,但实体框架
    上下文没有更新的值。第一次,只要刷新页面,该值,但是它第一次从来没有获得更新的值。

所以有人有任何人面临类似的问题吗?我在这里做错了什么?

So has anyone faced a similar issue anytime ? What am I doing wrong here ?

推荐答案

问题是EF不知道你的存储过程正在做什么,它?这个工作是在SQL Server中完成的。所以在你的存储过程执行后,你需要通过发出一个 Refresh() 通话:

The problem is that the EF does not know what your stored procedure is doing, how could it? That work is done at the SQL Server. So after your stored procedure executes, you need to ask EF to update that (and other related) instance by issuing a Refresh() call:

context.Refresh(RefreshMode.StoreWins, myObject);

StoreWins 告诉框架覆盖值在实例中使用数据库中的值。

The StoreWins tells the framework to overwrite values in the instance with values from the database.

这篇关于实体框架数据上下文不与数据库同步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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