请考虑以下主题:
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();
}
}