The closer equivalent is probably:
context.Configuration.AutoDetectChangesEnabled = false;
foreach (var contact in contacts)
{
context.Entry(contact).Collection(c => c.Addresses).Load();
Console.WriteLine(contact.Addresses.Count());
}
The .Entry
method calls DetectChanges
internally if AutoDetectChangesEnabled
is set to true
(which it is by default). Depending on the number of entities in the context and the number of contacts
automatic change detection can be rather time consuming, especially if called many times in a loop. In EF 4 and ObjectContext
there is no AutoDetectChangesEnabled
flag and change detection isn't performed in so many EF methods as it is in the DbContext
API when AutoDetectChangesEnabled
is true
.
Could you try if that improves the performance? It would interest me as well.