Search code examples
c#entity-frameworkasp.net-mvc-4ado.netentity-framework-4

Refresh the data in entity created using database-first approach model in Entity Framework


using (DEMONewEntities demonew = new DEMONewEntities())
{
}

In App.Config we have the following entry : enter image description here This is the structure generated in the Solution folder .

enter image description here

I have created the database initially. After that, I wrote the code from it using a database-first approach.

I have 2 requirements:

  1. How to refresh the data in the entities in C#, created using Entity Framework as shown in the code sample?

  2. How often to refresh the data as it may add to the performance?


Solution

  • By “refeshing the data”, I would assume that you mean creating a new context so Entity Framework get fresh data from the database instead of using entity loaded in the ChangeTracker.

    1.How to refresh the data in the entities in C#, created using Entity Framework as shown in the code sample?

    You usually create a new context. You can use multiple context in your code.

    using (DEMONewEntities demonew = new DEMONewEntities())
    {
    }
    
    // ...code...
    
    using (DEMONewEntities demonew = new DEMONewEntities())
    {
    }
    

    2.How often to refresh the data as it may add to the performance?

    As often as possible but that depend of what you do. Having to much entities in the Change Tracker can kill your performance.

    You can find some information about how slow the ChangeTracker can become with a lot of entities here: