为什么 Dapper 无法 return 插入多个行 ID?

Why is Dapper unable to return multiple inserted row ids?

我有一个 SQL 服务器 table,其中的行插入使用:

var sql = @"
DECLARE @InsertedRows AS TABLE (Id BIGINT);
INSERT INTO Person ([Name], [Age]) OUTPUT Inserted.Id INTO @InsertedRows
VALUES (@Name, @Age);
SELECT Id FROM @InsertedRows;";

Person person = ...;

var id = connection.Query<long>(sql, person).First();

这一切都很好,但是如果我尝试插入多个项目并且 return 所有插入的 ID 使用:

IEnumerable<Person> people = ...;    
var ids = connection.Query<long>(sql, people);

我收到一个错误:

System.InvalidOperationException : An enumerable sequence of parameters (arrays, lists, etc) is not allowed in this context
at Dapper.SqlMapper.GetCacheInfo(Identity identity, Object exampleParameters, Boolean addToCache)
at Dapper.SqlMapper.d__23`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---

如何在 Dapper 中插入一个 return 多个 ID?

某处某事需要循环。您有一个插入一行的 sql 语句,并在列表中发送代码。由于您喜欢字符串文字中的 SQL,我会坚持一次插入一个人,将循环放在 C# 中并使用 SELECT SCOPE_IDENTITY() 在 C# 中恢复每个 id。您不再需要 @InsertedRows 或 OUTPUT。

如果您真的想在 SQL 中循环,我相信您需要查看 table 值参数来传递您的插入列表。 Dapper 很乐意 return 多个 Id。它抱怨多个人作为输入参数。

有一天,希望很快,我们将回顾字符串文字中的 SQL,就像我们目前看 goat sacrifice.