简体   繁体   中英

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.

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;

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://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.dbcontextoptionsbuilder.usequerytrackingbehavior?view=efcore-3.1

Pretty sure this is what you're looking for

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM