在小巧玲珑的空TVP

时间:2018-01-23 18:34:25

标签: dapper

我创建了一个接收一些基元参数和一个TVP的过程。 我创建了一个收到List<T>的图书馆,并为TVP(https://github.com/Kunze/EasyTVP)返回List<SqlDataRecord>。它有效,除非我的TVP为空/无效。

当tvp为null时,我得到(因为在Windows服务器中,因为TVP不能为null):

"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方法抛出了这个错误(如果它是一个类,它可以工作,但不适用于动态):

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

如果我创建2个类,我可以得到这个工作,一个用于Products.Count&gt;对于Products = null或0,0和一个不同,但这很荒谬,对吗?

那么,我怎样才能使这个有用呢?

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

1 个答案:

答案 0 :(得分:0)

I could do it works calling AsTableValuedParameter:

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