将其设置为false后立即启用AutoDetectChangesEnabled

时间:2018-07-18 07:43:16

标签: c# entity-framework entity-framework-6

请考虑以下主题:

here

Entity Framework Automatic Detect Changes

我有一个问题。为什么在这两篇文章中都着重强调了启用 AutoDetectChangesEnabled设置为false后。如果我只想批量插入,为什么必须在SaveChange之后将其设置为true?是否会影响其余删除或更新记录的方法?例如:

public void BulkInsert(List<Blog> aLotOfBlogs)
{
    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); 
           } 

           context.SaveChange();
        } 
        finally 
        { 
            context.Configuration.AutoDetectChangesEnabled = true; 
        } 
    }
}

public void Update(int id)
{
    using (var context = new BloggingContext()) 
    { 
        var record = context.Blogs.Where(o=>o.Id == id).First(); 
        record.Body = "Some Text";
        context.SaveChange();
    }
}

0 个答案:

没有答案