Entity Framework - Local Data

Running a LINQ query directly against a DbSet will always send a query to the database, but you can access the data that is currently in-memory using the DbSet.Local property.

The Local property of DbSet provides simple access to the entities of the set that are currently being tracked by the context and have not been marked as Deleted. Accessing the Local property never causes a query to be sent to the database. This means that it is usually used after a query has already been performed. The Load extension method can be used to execute a query so that the context tracks the results.


using (var context = new BooksContext()) 
    // Load all books from the database into the context 
    // Add a new book to the context 
    context.Books.Add(new Book { Name = "New Book" }); 
    // Mark one of the existing books as Deleted 
    // Loop over the books in the context. 
    Console.WriteLine("In Local: "); 
    foreach (var book in context.Books.Local) 
        Console.WriteLine("Found {0}: {1} with state {2}",book.BookId, book.Name, 
    // Perform a query against the database. 
    Console.WriteLine("\nIn DbSet query: "); 
    foreach (var book in context.Books) 
        Console.WriteLine("Found {0}: {1} with state {2}",book.BookId,  book.Name, 

Praesent mattis

Pellentesque viverra vulputate enim. Aliquam erat volutpat. Pellentesque tristique ante ut risus. Quisque dictum. Integer nisl risus, sagittis convallis, rutrum id, elementum congue, nibh. Suspendisse dictum porta lectus. Donec placerat odio vel elit.

Read More