Entity Framework 4.1 - How to “Force” EF To Go To DB Instead of Using Graph?

混江龙づ霸主 提交于 2019-12-23 09:17:35

问题


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

  1. Grab an object (let's say "Tag")
  2. Save some other object (let's say "Question")
  3. Get a fresh copy of "Tag".
  4. Redirect to another page, which needs a fresh copy of "Tag".

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

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.

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

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.

Is there any way to say:

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

Or is there another alternative?

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

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).


回答1:


Found my answer, i think:

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

Trying now...



来源:https://stackoverflow.com/questions/7735291/entity-framework-4-1-how-to-force-ef-to-go-to-db-instead-of-using-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!