在 EF Core 3.1 中执行更新 - 输出查询
Performing UPDATE - OUTPUT query in EF Core 3.1
在 EF Core 2 中,我能够使用 FromSql
并发出 UPDATE-OUTPUT
查询在单个查询中更新和检索实体。
看起来像这样:
return await _baseDbContext.MyEntity
.FromSql($@"UPDATE {_baseDbContext.SchemaName}.MyEntity
SET STATUS = ""InProgress"",
UpdatedAt = SYSDATETIMEOFFSET()
OUTPUT
INSERTED.Id, INSERTED.Name,
INSERTED.Status, INSERTED.Deleted, INSERTED.CreatedAt, INSERTED.CreatedBy, INSERTED.CreatedByUserId,
INSERTED.UpdatedAt, INSERTED.UpdatedBy, INSERTED.UpdatedByUserId, INSERTED.Version
FROM { _baseDbContext.SchemaName}.MyEntity le
WHERE le.Status = ""Failed""")
.ToListAsync(cancellationToken);
现在升级到 EF Core 3 后,抱怨
"FromSqlRaw or FromSqlInterpolated was called with non-composable SQL and with a query composing over it. Consider calling AsEnumerable
after the FromSqlRaw or FromSqlInterpolated method to perform the composition on the client side.
如果我只输入 SELECT
查询,我可以执行 FromSqlRaw
或 FromSqlInterpolated
。
EF core 3 不再支持 UPDATE-OUTPUT
查询了吗?
根据此处发布的答案添加 .IgnoreQueryFilters()
解决了这个问题:
在 EF Core 2 中,我能够使用 FromSql
并发出 UPDATE-OUTPUT
查询在单个查询中更新和检索实体。
看起来像这样:
return await _baseDbContext.MyEntity
.FromSql($@"UPDATE {_baseDbContext.SchemaName}.MyEntity
SET STATUS = ""InProgress"",
UpdatedAt = SYSDATETIMEOFFSET()
OUTPUT
INSERTED.Id, INSERTED.Name,
INSERTED.Status, INSERTED.Deleted, INSERTED.CreatedAt, INSERTED.CreatedBy, INSERTED.CreatedByUserId,
INSERTED.UpdatedAt, INSERTED.UpdatedBy, INSERTED.UpdatedByUserId, INSERTED.Version
FROM { _baseDbContext.SchemaName}.MyEntity le
WHERE le.Status = ""Failed""")
.ToListAsync(cancellationToken);
现在升级到 EF Core 3 后,抱怨
"FromSqlRaw or FromSqlInterpolated was called with non-composable SQL and with a query composing over it. Consider calling
AsEnumerable
after the FromSqlRaw or FromSqlInterpolated method to perform the composition on the client side.
如果我只输入 SELECT
查询,我可以执行 FromSqlRaw
或 FromSqlInterpolated
。
EF core 3 不再支持 UPDATE-OUTPUT
查询了吗?
根据此处发布的答案添加 .IgnoreQueryFilters()
解决了这个问题: