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

1 thought on “AsNoTracking() to force your data context to get everything from the database

  1. Pingback: Patrick Desjardins' Blog » How to include correctly property within a collection when using Entity Framework code first

Leave a Reply to Patrick Desjardins' Blog » How to include correctly property within a collection when using Entity Framework code first Cancel 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.