我已经创建了一个接收一些原语参数和一个TVP的过程。我已经创建了一个库,它接收List<T>并返回List<SqlDataRecord> (https://github.com/Kunze/EasyTVP)。它可以工作,除非我的TVP为空/null。
当tvp为null时,I get (因为在sql server中TVP不能为null ):
"Table Valued Parameter cannot be null"但是如果你不传递空的参数,TVP就会起作用,所以我试着使用一个像这样的扩展(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"}如果我创建两个类,一个类用于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);发布于 2018-01-24 03:35:16
我可以通过调用AsTableValuedParameter来实现
if(model.Products != null && model.Products.Count > 0){
dynamicParameters.Add("Products", list_of_sql_data_record.AsTableValueParameter());
}https://stackoverflow.com/questions/48408575
复制相似问题