0

I am using Entity Framework code-first and I call edit user, inside edit user I call a stored procedure that is editing the user. After that, I call another method to get the user from the database.

The problem is that this method returns the old user so I think I need to refresh the context before calling the second method. How I can refresh the context in Entity Framework?

Based on this page: Entity Framework Refresh context?

I try to use this:

 yourContext.Entry(yourEntity).Reload();

but I'm not sure how to do this exactly.

This is my context:

var newUser = await authenticationContext.Users.Where(u => u.Email == "user1@test.com").FirstOrDefaultAsync();

I tried:

 authenticationContext.Entry(authenticationContext.Users).Reload();

but it is throwing an error.

System.InvalidOperationException: the entity type Dbset is not part of the model for the current context.

Community
  • 1
  • 1
Alma
  • 3,780
  • 11
  • 42
  • 78
  • ***WHAT*** error !?!?!? – marc_s Dec 29 '16 at 21:42
  • You haven't said what database you're using, but why not just have the stored procedure return the updated entity instead of doing another query? – Heretic Monkey Dec 29 '16 at 22:03
  • Why do you want to re-fetch the user? You just saved it, so it must be identical to the database content. Or does it contain computed columns? – Gert Arnold Dec 29 '16 at 22:05
  • @GertArnold no does not, but when I update and try to get it, it return me back the old context. – Alma Dec 29 '16 at 22:46
  • WHY do you want to get it after saving? You should show more code to demonstrate that this is necessary. – Gert Arnold Dec 29 '16 at 22:49
  • `authenticationContext.Entry(newUser).Reload();` should do, although as Gert mentioned, it's unclear why do you need that. – Ivan Stoev Dec 30 '16 at 00:25

1 Answers1

0

Have you tried reinitializing the context?

var newUser = await authenticationContext.Users.Where(u => u.Email == "user1@test.com").FirstOrDefaultAsync();
// do cool stuff with sprocs and whatnot here
authenticationContext = new AuthenticationContext(); // Or whatever your context model looks like
var newUser = await authenticationContext.Users.Where(u => u.Email == "user1@test.com").FirstOrDefaultAsync();

Or simply by using "usings" where you want always fresh data, instead of keeping the context as a field?

using (var authenticationContext = new AuthenticationContext())
{
    var newUser = await authenticationContext.Users.Where(u => u.Email == "user1@test.com").FirstOrDefaultAsync();
}

The method you've tried is for reloading a single entity that exists in EF's tracker. You can't supply a DbSet to that method. I suppose you would use that method if you had an entity attached to the tracker:

var newUser = await authenticationContext.Users.Where(u => u.Email == "user1@test.com").FirstOrDefaultAsync();
// do cool stuff with sprocs and whatnot here
authenticationContext.Entry(newUser).Reload();

As mentioned in that answer though, you might need to think about navigation props. Also you need to keep in mind that if you change that value of what EF is using to track entities (I suppose it's the primary key, but I'm not sure), you might get unwanted results.

So that's what you can do with code. But you should clearly consider what's suggested in the comments, having to fetch data you should already have indicates code smell. Also I haven't tested any of this, hopefully it works and/or you get the gist of it.

naslund
  • 615
  • 1
  • 8
  • 15