DbContext AutoDetectChangesEnabled set to false detecting changes

后端 未结 3 501
广开言路
广开言路 2020-12-02 16:48

I\'m a bit stumped. From what I\'ve read setting the DbContext.AutoDetectChangesEnabled to false should disable change tracking requiring one to ca

3条回答
  •  旧巷少年郎
    2020-12-02 17:34

    according to Entity Framework Automatic Detect Changes's Article

    they said:

    you may get significant performance improvements by turning it off in some cases

    look at this example from that article

    using (var context = new BloggingContext()) 
    { 
        try 
        { 
            context.Configuration.AutoDetectChangesEnabled = false; 
    
            // Make many calls in a loop 
            foreach (var blog in aLotOfBlogs) 
            { 
                context.Blogs.Add(blog); 
            } 
        } 
        finally 
        { 
            context.Configuration.AutoDetectChangesEnabled = true; 
        }
    }
    

    This code avoids unnecessary calls to DetectChanges that would have occurred while calling the DbSet.Add and SaveChanges methods.

提交回复
热议问题