开发者

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

开发者 https://www.devze.com 2023-04-12 04:29 出处:网络
Here\'s the scenario, i have a website, which in a single 开发者_StackOverflowHTTP request (HTTP POST), i need to do the following:

Here's the scenario, i have a website, which in a single 开发者_StackOverflowHTTP 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).


Found my answer, i think:

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

Trying now...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号