的EntityFramework力每次查询前全球重装 [英] EntityFramework force globally reload before each query

查看:99
本文介绍了的EntityFramework力每次查询前全球重装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 DBEntities.edmx 从我的数据库。在我的班级之一,我用:

  DBEntities _db =新DBEntities()

现在是我的问题,即查询后EF缓存解决方案,如果在数据库中的一个值改变的 的.edmx 使用缓存的旧值。

我发现了两种解决方法:

1: _db.tbl_user.AsNoTracking(),其中(X => x.ID == _ID)。

2:重建DBEntities: _db =新DBEntities()

但我想知道是否有在全球范围内强制,即EF更新DBEntities如果值发生了变化的可能。


解决方案

  

您守信_db变量,并用它所有的时间?然后
  别。处置它(使用using语句pferably $ P $),并创建一个新的
  比如,每次你的DbContext的。


  
  

数据上下文不设计为长寿命。重复使用它在多个业务是一个错误在您的设计


要解决需要using语句的问题:

 使用(DBEntities _db =新DBEntities())
{
    / *在这里你DB访问* /
}

您可以找到的我总是在我的DbContext对象调用Dispose()?

I have a DBEntities.edmx from my Database. In one of my Classes I use:

DBEntities _db = new DBEntities()

Now is my Problem, that after a query the EF caches the solution and if one value in the Database changes the .edmx uses the cached old value.

I found two workarounds:

1.: _db.tbl_user.AsNoTracking().Where(x=>x.ID == _ID)

2.: recreate the DBEntities: _db = new DBEntities()

but I want to know if there is a possibility to force globally, that the EF update the DBEntities if the values had changed

解决方案

are you keeping one _db variable and using it all the time? then don't. Dispose it (preferably with a using statement) and create a new instance of your DbContext each time.

The data context is not designed to be long-lived. Reusing it over multiple operations is a mistake in your design

To solve the problem the using statement is needed:

using(DBEntities _db = new DBEntities())
{
    /* Your DB access here */
}

You can find a full description on Do I always have to call Dispose() on my DbContext objects?

这篇关于的EntityFramework力每次查询前全球重装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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