如何使用EF Core SqlServer生成此查询?
UPDATE Table1
SET Field1 = sil.Field1,
Field2 = sil.Field2
FROM Table2 sil WITH (NOLOCK)
WHERE sil.Field3= ''
AND sil.Id1 = Table1.Id1
AND sil.Id2 = Table1.Id2
AND sil.Id3 != Table1.Id3
重要!
我想用单个查询(不是每行查询)生成它
答案 0 :(得分:0)
AFAIK与EF中的 UPDATE ... FROM 语句没有直接等效关系。相反,您将查询数据集,更新实体模型,然后将更新推回。
// 2 database select statements
var table1 = database.Table1.ToList();
var table2 = database.Table2.ToList();
// in-memory processing. no database involvement.
foreach(Entity1 t1record in table1) {
var t2record = table2.Where(sil => ...).FirstOrDefault();
if(null == t2record) { continue; }
t1record.Field1 = t2record.Field1; // etc...
}
// push changes back to the database. EFCore should do this as a single SQL statement
database.SaveChanges();