Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to control AutoDetectChanges on SqlEntityConnection?

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?

like image 436
Joel Mueller Avatar asked Jan 02 '13 23:01

Joel Mueller


People also ask

How to disable change tracking in entity Framework?

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.

What does AutoDetectChangesEnabled do?

It just disables the automatic call of DetectChanges that would otherwise occur in many DbContext API methods.

How does Entity Framework detect changes?

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.


1 Answers

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

like image 175
Not loved Avatar answered Sep 17 '22 22:09

Not loved