Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable AutoDetectChanges on Entity Framework Core

someone knows how to disable the AutoDetectChanges on EFCore?

I need to do it because I have to make an huge import in my database, and can't find information on web.

Tried this but it's not working:

_context.Configuration.AutoDetectChangesEnabled = false;

Say configuration does not exist.

Thanks a lot.

like image 313
Rob None Avatar asked Jan 09 '20 16:01

Rob None


People also ask

How do I turn off 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?

AutoDetectChangesEnabled to false should disable change tracking requiring one to call DbContext. DetectChanges in order to identify changes to be sent to the database.

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

What you have tried

_context.Configuration.AutoDetectChangesEnabled = false;

is for EF6.

The corresponding EF Core option AutoDetectChangesEnabled is property of the ChangeTracker associated with the DbContext, so the corresponding code is

_context.ChangeTracker.AutoDetectChangesEnabled = false;
like image 171
Ivan Stoev Avatar answered Sep 21 '22 14:09

Ivan Stoev