0

When I open my program and try to get all my users the following code will be executed:

    public static List<User> GetAllUsers()
    {
        return db.Users.OrderBy(x => x.ID).ToList();
    }

If I go to my SQL Server Management Studio and change some value and redo GetAllUsers(); the data is still the same. Is there any way to refresh the data? Best would be to even refresh all (I have other classes in my DbContext) so i actually reload everything.

Andreas
  • 116
  • 5

1 Answers1

1

Check this question How to Refresh DbContext

But i suggest not to use a static dbcontext,check this
Pros and Cons of putting a db context in static class library

Community
  • 1
  • 1
George Vovos
  • 7,563
  • 2
  • 22
  • 45
  • I was only working with a static dbcontext so I am not sure if i can change this right now. By the way, the data refresh if i call db = new Database(); .. but i think this is not the right way to handle this, is it? – Andreas Aug 08 '14 at 19:41
  • Either that,or use the Refresh method as it is shown in my first link. – George Vovos Aug 08 '14 at 19:46
  • That works aswell. But i think if i Dispose() my db and create it again this will work without detecting all changes. But your answer is right aswell :) – Andreas Aug 08 '14 at 19:50
  • if you dispose the context and create a new one it will definitely load the latest data – George Vovos Aug 08 '14 at 19:52