实体框架4.1-如何“强制"执行. EF转到DB而不是使用图? [英] Entity Framework 4.1 - How to "Force" EF To Go To DB Instead of Using Graph?

查看:127
本文介绍了实体框架4.1-如何“强制"执行. EF转到DB而不是使用图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种情况下,我有一个网站,在一个HTTP请求(HTTP POST)中,我需要执行以下操作:

Here's the scenario, i have a website, which in a single HTTP request (HTTP POST), i need to do the following:

  1. 抓取一个对象(比如说标签")
  2. 保存其他对象(比如说问题")
  3. 获取标签"的新副本.
  4. 重定向到另一个页面,该页面需要新的标签"副本.

幕后2)涉及数据库端触发器,这些触发器会影响标签"上的数据.

Behind the scenes, 2) involves database-side triggers that affects data on "Tag".

因此,当我执行3)时,EF从步骤1)中提取对象的相同副本,因为它位于图形/内部存储器中(例如,相同的连接/上下文)

So when i do 3), EF is pulling the same copy of the object from step 1), since it's in the graph/internal memory (e.g same connection/context)

我需要该对象的新"副本.

I need a "fresh" copy of the object.

过去,我曾经使用过Detach,然后我执行EF查询并从数据库中获取最新的对象.

In the past, i've used Detach, then i perform an EF query and the latest object in fetched from the DB.

但是我本身无法访问这里的对象(我有一个DTO,它从我的存储库中返回),所以我没有任何东西可以传递给Detach方法.

But i don't have access to the object here per-se (i have a DTO, which is returning from my repository), so i don't have anything to pass to the Detach method.

有什么办法说:

var fresh = db.Tags.Find(1, ignoreGraph: true)

还是有另一种选择?

如上所述,我使用的是Entity Framework 4.1,C#4(和ASP.NET MVC 3)

As mentioned, i'm on Entity Framework 4.1, C# 4 (and ASP.NET MVC 3)

我现在唯一看到的解决方案是将querystring参数传递给下一页,然后该页将获取新副本(因为它是新的上下文,新的图形等).

The only solution i can see right now is to pass a querystring parameter to the next page, which then grabs the fresh copy (since it's a new context, new graph, etc).

推荐答案

找到我的答案,我认为:

Found my answer, i think:

Context.Entry<T>(entity).Reload()

立即尝试...

这篇关于实体框架4.1-如何“强制"执行. EF转到DB而不是使用图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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