问题
Here's the scenario, i have a website, which in a single HTTP request (HTTP POST), i need to do the following:
- Grab an object (let's say "Tag")
- Save some other object (let's say "Question")
- Get a fresh copy of "Tag".
- 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