Entity Framework 4.1 - How to "force" EF to move to the database instead of using a graph?

Here is the scenario, I have a website that in one HTTP request (HTTP POST) I need to do the following:

  • Take an object (say, "Tag")
  • Save another object (say "question")
  • Get a new copy of the tag.
  • Redirect to another page for which you need a new copy of the "Tag".

Behind the scenes, 2) includes triggers on the database side that affect the data in the "tag".

So, when I do 3), EF pulls out the same copy of the object from step 1), since it is in the graph / internal memory (for example, in the same connection / context)

I need a "fresh" copy of the object.

In the past I used Detach , then I execute an EF query and the last object retrieved from the database.

But I donโ€™t have access to the object here per-se (I have a DTO that comes back from my repository), so I have nothing to pass to the Detach method.

Is it possible 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 the querystring parameter to the next page, which then captures a new copy (since this is a new context, new graph, etc.).

+6
source share
1 answer

Found my answer, I think:

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

Now try ...

+13
source

Source: https://habr.com/ru/post/899096/


All Articles