Of course, before we can work with the state metadata, we need to obtain a reference to the object that represents it. Both the ObjectContext
and DbContext
expose methods to retrieve the state entry for a specific entity or a collection of entries:
To retrieve the state entry for a specific entity, you use the ObjectStateManager.GetObjectStateEntry()
method in the ObjectContext
API, or the Entry()
method of the DbContext
:
The ObjectContext
provides an ObjectStateManager.GetObjectStateEntries()
method that retrieves all the entries by their state. To filter the results by entity type or set, you can use LINQ operators on the result. (This is the method and the technique we used when we were retrieving in-memory entities in the last chapter.)
myObjectContext.ObjectStateManager.GetObjectStateEntries(EntityState.Added)
The DbContext
API provides two overloads of the DbChangeTracker
method for retrieving multiple entries. You can either retrieve state entries for all the entities being tracked by the context, or for all entries of a specific type.
Put On Your Thinking Hat
Based on the examples I’ve provided, can you write code snippets to retrieve the following state entries?
Using ObjectContext:
Retrieve the state entries for Recipe
entities that have been changed since the last time SaveChanges()
was called.
Retrieve the state entry for mySupplier
.
Using Dbcontext
Retrieve the state entries for Recipe
entities that have been changed since the last time SaveChanges()
was called.
Retrieve the state entry for mySupplier.
Make A Note
The ObjectContext
API’s ObjectStateEntry
lives in the System.Data.Objects
namespace. The DbContext
API’s DbEntityEntry
lives in System.Data.Entities.Infrastructure
. You’ll want to add references (using
or Imports
statements) to your code files when you use them.
Put On Your Thinking Hat
How’d you do?
Using ObjectContext:
Retrieve the state entries for Recipe
entities that have been changed since the last time SaveChanges()
was called.
IEnumerable<ObjectStateEntry> rEntries = myContext.ObjectStateManager
.GetObjectStateEntries(EntityState.Modified)
.where (e => e.Entity is Recipe)
.select e;
This is a lot like the code in the last chapter, but we’re not translating the type.
Retrieve the state entry for mySupplier
.
ObjectStateEntry rState = myContext.ObjectStateManager
.GetObjectStateEntry(mySupplier);
Using Dbcontext
Retrieve the state entries for Recipe
entities that have been changed since the last time SaveChanges()
was called.
IEnumerable<DbEntityEntry> rEntries = myContext.DbChangeTracker
.Entries<Recipe>()
.where (e => e.State == EntityState.Modified)
.select e;
Retrieve the state entry for mySupplier
.
ObjectStateEntry rState = myContext.Entry(mySupplier);
Using ObjectContext:
Retrieve the state entries for Recipe
entities that have been changed since the last time SaveChanges()
was called.
Dim rEntries As IEnumerable(Of ObjectStateEntry) = myContext.ObjectStateManager _
.GetObjectStateEntries(EntityState.Modified) _
.where (Function(e) e.Entity is Recipe) _
.select e
Retrieve the state entry for mySupplier
.
Dim rState As ObjectStateEntry = myContext.ObjectStateManager _
.GetObjectStateEntry(mySupplier)
Using Dbcontext
Retrieve the state entries for Recipe
entities that have been changed since the last time SaveChanges()
was called.
Dim rEntries As IEnumerable(Of DbEntityEntry) = myContext.DbChangeTracker _
.Entries<Recipe>() _
.where (Function(e) e.State = EntityState.Modified) _
.select e
Retrieve the state entry for mySupplier
.
Dim rState As ObjectStateEntry = myContext.Entry(mySupplier);