Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在EF dbcontext中一次更新多个表_C#_Entity Framework_Asp.net Core 1.1 - Fatal编程技术网

C# 在EF dbcontext中一次更新多个表

C# 在EF dbcontext中一次更新多个表,c#,entity-framework,asp.net-core-1.1,C#,Entity Framework,Asp.net Core 1.1,我在EF示例中没有遇到任何where,但一次更新多个表是否安全,如下所示 using(var db = new MyDbContext(opts)) { var record = db.Record.FirstOrDefault(x => x.Id == id); if(record == null) return; record.FieldN = ... db.Update(record); db.Other.RemoveRange(db.Other.Wher

我在EF示例中没有遇到任何where,但一次更新多个表是否安全,如下所示

using(var db = new MyDbContext(opts))
{
  var record = db.Record.FirstOrDefault(x => x.Id == id);

  if(record == null) return;

  record.FieldN = ...

  db.Update(record);

  db.Other.RemoveRange(db.Other.Where(x => x.EntityId == id));

 db.SaveChanges();
}
是的,它是安全的


db.SaveChanges()
将在原子事务中执行所有更新/插入/删除查询。它将使用当前事务或创建新事务

是的,但有时EF会以错误的顺序执行您的请求,然后它会爆炸,例如,删除两个相互依赖的内容


解决方案是通过多次调用savechangessync()将它们分开(您实际上应该使用它而不是SaveChanges)。

我不确定依赖项会如何影响删除?多个调用是指在上述情况下使用(var db=…)的两个