This article provides some evidence that turning off AutoDetectChanges on your Entity Framework data context can provide a significant performance improvement when inserting large numbers of entities.
context.Configuration.AutoDetectChangesEnabled = false;
However, the DataContext provided by the SqlEntityConnection type provider doesn't seem to provide any way to control this setting.
There's no context.Configuration
property, or context.DataContext.Configuration
property. There is a context.DataContext.ContextOptions
but it has nothing even resembling AutoDetectChangesEnabled
.
The DataContext
property on the type provider context is of type System.Data.Objects.ObjectContext
. Does anyone know of a way to influence this particular setting from there?
In Entity Framework, change tracking is enabled by default. You can also disable change tracking by setting the AutoDetectChangesEnabled property of DbContext to false. If this property is set to true then the Entity Framework maintains the state of entities.
It just disables the automatic call of DetectChanges that would otherwise occur in many DbContext API methods.
Detect Changes works by detecting the differences between the current property values of the entity and the original property values that are stored in a snapshot when the entity was queried or attached. The techniques shown in this topic apply equally to models created with Code First and the EF Designer.
I wrote a pretty similar article last year on detect changes performance which you can find here: http://blog.staticvoid.co.nz/2012/5/7/entityframework_performance_and_autodetectchanges My experience is mostly with DbContext (which wraps ObjectContext) but i did a bit of a search and found the following
Why is inserting entities in EF 4.1 so slow compared to ObjectContext?
what this says is that ObjectContext doesnt actually do automatic change detection so this isnt something you should need to worry about. However you still do need to be aware that large object graphs will slow things down as is all snapshot tracking scenarios detect changes is required at some point, and this involves full enumeration of the object graph
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With