我有一个更新对象列表。我试图只用一个数据库调用来更新所有记录。(一个 context.SaveChanges() 命令)。该程序在 Windows 服务中运行。我的问题不是更新,而是将数据库中的所有记录相乘并添加重复项。以下是我的代码。
//adding the list of updated customer objects to the list
context_CLTUS.Customer_Updates.AddRange(list.customerUpdateList);
foreach (var j in list.customerUpdateList)
{
//calling entity state modify for each record
context_CLTUS.Entry(j).State = EntityState.Modified;
}
//save updated list in one db call.
context_CLTUS.SaveChanges();
我无法阐明为什么所有记录都在复制而不是更新。你能解释一下并帮助我吗?谢谢。
最佳答案
引用这个answer
您可以尝试这些解决方案:
TransactionScope 中的交换更新操作。
设置 Configuration.AutoDetectChangesEnabled = false。
尝试 Entity Framework Plus , 高性能增强 图书馆。
关于c# - 批量更新记录 Entity Framework 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45114892/