Database Reference
In-Depth Information
static void RunExample()
{
using (var context = new EF6RecipesContext())
{
var tech1 = new Technician { Name = "Julie Kerns" };
var tech2 = new Technician { Name = "Robert Allison" };
context.ServiceCalls.Add(new ServiceCall {
ContactName = "Robin Rosen",
Issue = "Can't get satellite signal.",
Technician = tech1 });
context.ServiceCalls.Add(new ServiceCall {
ContactName = "Phillip Marlowe",
Issue = "Channel not available",
Technician = tech2 });
// now get the entities we've added
foreach (var tech in
context.ChangeTracker.GetEntities<Technician>())
{
Console.WriteLine("Technician: {0}", tech.Name);
foreach (var call in tech.ServiceCalls)
{
Console.WriteLine("\tService Call: Contact {0} about {1}",
call.ContactName, call.Issue);
}
}
}
}
}
public static class ChangeTrackerExtensions
{
public static IEnumerable<T> GetEntities<T>(this DbChangeTracker tracker)
{
var entities = tracker
.Entries<T>()
.Where(entry => entry.State != EntityState.Detached && entry.Entity != null)
.Select(entry => entry.Entity)();
return entities;
}
}
Following is the output of the code in Listing 7-4:
Technician: Julie Kerns
Service Call: Contact Robin Rosen about Can't get satellite signal.
Technician: Robert Allison
Service Call: Contact Phillip Marlowe about Channel not available
 
Search WWH ::




Custom Search