Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 206
RPM1984 Avatar asked Oct 12 '11 04:10

RPM1984


People also ask

Which class of Entity Framework is used to represent data table?

DbSet in Entity Framework 6. The DbSet class represents an entity set that can be used for create, read, update, and delete operations. The context class (derived from DbContext ) must include the DbSet type properties for the entities which map to database tables and views.


1 Answers

Found my answer, i think:

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

Trying now...

like image 156
RPM1984 Avatar answered Oct 02 '22 21:10

RPM1984