Disable AutoDetectChanges on Entity Framework Core
Asked Answered
M

3

19

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.

Manno answered 9/1, 2020 at 16:24 Comment(3)
ORMs are NOT meant for huge imports. Besides, you do want to track changes when importing - otherwise the ORM won't know what to insertBrnaby
PS you already asked this. The answer remains the same. Don't use an ORM. You have no Objects to Map to Relational tables, you only have rows. Using an ORM to insert 500K rows will result in roughly 500-500K times worse performance, depending on whether the ORM can batch INSERTs or not.Brnaby
Even with EF Core 2.x's INSERT batching, you can't include more than 1K rows per batch, resulting in 500 separate INSERTs at best. All of those rows will be fully logged, resulting in a lot of IO and adding at least 2GB to the transaction log per 2GB file. SqlBulkCopy on the other hand will send all rows in a single stream using minimal loggingBrnaby
F
27

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;
Fawcette answered 9/1, 2020 at 18:45 Comment(0)
B
25

I think the way I've done it before is when you register your DBContext you can turn it off so that you don't have to add it to every query.

Off the top of my head and don't have code ex. to reference right now so I could be wrong

services.AddDbContext<YourDbContext>(options =>
{
    options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
});

EDIT: Found it. https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.dbcontextoptionsbuilder.usequerytrackingbehavior?view=efcore-3.1

Pretty sure this is what you're looking for

Breda answered 9/1, 2020 at 16:35 Comment(1)
Switching off tracking is not the same as turning off automatic change detection. The latter is typically used to postpone change detection on tracked entities in order to win performance. I.e. at a later moment change detection will be executed explicitly in the code.Forras
C
5

This is what I'm familiar with, from the docs:

var blogs = context.Blogs
    .AsNoTracking()
    .ToList();

Ref: https://learn.microsoft.com/en-us/ef/core/querying/tracking

Corry answered 9/1, 2020 at 16:27 Comment(4)
Thanks a lot! And if i use this one? _context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;Manno
The OP wants to insert data. Disabling change tracking will prevent thatBrnaby
The two steps are connected. If you query data with no tracking then insert / update behavior with those entities will be affected. The downside of doing it is that EF will try to insert a record that already exists, or update a record that doesn't exist and get an error. I would assume OP's strategy accounts for that.Corry
Switching off tracking is not the same as turning off automatic change detection. The latter is typically used to postpone change detection on tracked entities in order to win performance. I.e. at a later moment change detection will be executed explicitly in the code.Forras

© 2022 - 2024 — McMap. All rights reserved.