Home » Ado.Net » Entity Framework » AsNoTracking() to force your data context to get everything from the database

AsNoTracking() to force your data context to get everything from the database

You can specify Entity Framework to get everything back from the database instead of using what has been already loaded from the database.

A use case would be that you attach an entity from the web form and you save everything. You may return the view from the entity loaded but this one won’t get the full object if it has been attached previously.

        
public Workout Get(int id)
{
     return DatabaseContext
              .SetOwnable<Workout>()
              .Include(x => x.Sessions)
              .Include("Sessions.WorkoutSessionExercises")
              .Include("Sessions.WorkoutSessionExercises.Exercise")
              .AsNoTracking()
              .Single(c => c.Id == id);
}

With the addition of AsNoTracking, the information won’t be get from the local storage of the database context but from the database.

If you like my article, think to buy my annual book, professionally edited by a proofreader. directly from me or on Amazon. I also wrote a TypeScript book called Holistic TypeScript

One Response so far.

  1. […] have several example in this website that include with a string the property when it’s a property inside a […]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.