Dapper 中的空 TVP

Empty TVP in Dapper

我创建了一个接收一些原语参数和一个 TVP 的过程。 我创建了一个库,它接收一个 List<T> 和 returns 一个 List<SqlDataRecord> 用于 TVP (https://github.com/Kunze/EasyTVP)。它有效,除非我的 TVP 是 empty/null.

当 tvp 为空时我得到(因为 TVP 在 sql 服务器中不能为空):

"Table Valued Parameter cannot be null"

但是如果你不传递参数为 null,TVP 就会工作,所以我尝试使用这样的 expando(DynamicParameters 的相同错误):

dynamic expando = new ExpandoObject();
...
if(model.Products != null && model.Products.Count> 0){
    expando.Products = list_of_sql_data_record;
}

但是 dapper 的 Query 方法会抛出这个错误(如果它是 class 就可以,但对于动态则不行):

{"The member  of type Microsoft.SqlServer.Server.SqlDataRecord cannot be used as a parameter value"}

如果我创建 2 个 classes,一个用于 Products.Count > 0,另一个用于 Products = null 或 0,我可以完成这项工作,但这很荒谬,对吧?

那么,我怎样才能让它发挥作用?

conexao.Query(@"Add", new
{
     model.Name,
     Products = list_of_sql_data_record //this may be empty
}, commandType: System.Data.CommandType.StoredProcedure);

我可以调用 AsTableValuedParameter:

if(model.Products != null && model.Products.Count > 0){
     dynamicParameters.Add("Products", list_of_sql_data_record.AsTableValueParameter());
}