Change Tracker: Working with tracking information (Multiple Entities)

Description and code samples
Table of Contents
Description

 

Class Method / Property Type Description Comments
DbContext ChangeTracker DbChangeTracker Provides access to features of the context that deal with change tracking of entities
DbChangeTracker Entries
Entries<TEntity>
IEnumerable<DbEntityEntry<TEntity>> Gets DbEntityEntry objects for all the entities of the given type tracked by this context

 

Operations
How to get tracked entities
How to do it
using (var context = new BreakAwayContext())
  {
    var res = (from r in context.Reservations
               where r.Trip.Description == "Trip from the database"
               select r).Single();

    context.Entry(res)
      .Collection(r => r.Payments)
      .Load();

    res.Payments.Add(new Payment { Amount = 245 });

    var entries = context.ChangeTracker.Entries();
    foreach (var entry in entries)
    {
      Console.WriteLine(
        "Entity Type: {0}",
        entry.Entity.GetType());

      Console.WriteLine(
        " - State: {0}",
        entry.State);
    }
How to get tracked entities of specific type
How to do it
using (var context = new BreakAwayContext())
{
  var entries = context.ChangeTracker.Entries<Payment>();
  foreach (var entry in entries)
  {
    Console.WriteLine(
      "Amount: {0}",
      entry.Entity.Amount);

    Console.WriteLine(
      " - State: {0}",
      entry.State);
  }
}