I have created a procedure that receives some primitives parameters and one TVP.
I have created a library that receives a List<T>
and returns a List<SqlDataRecord>
for the TVP (https://github.com/Kunze/EasyTVP). And it works, except when my TVP is empty/null.
When the tvp is null I get (becausa TVP cannot be null in sql server):
"Table Valued Parameter cannot be null"
But TVP works if you dont pass the parameter if it's null, so I tried to use a expando like this (the same error for DynamicParameters):
dynamic expando = new ExpandoObject();
...
if(model.Products != null && model.Products.Count> 0){
expando.Products = list_of_sql_data_record;
}
but the Query method of dapper throws this error (it works if it is a class, but not for dynamic):
{"The member of type Microsoft.SqlServer.Server.SqlDataRecord cannot be used as a parameter value"}
I can get this work if I create 2 classes, one for Products.Count > 0 and one different for Products = null or 0, but this is ridiculous, right?.
So, How can I make this works?
conexao.Query(@"Add", new
{
model.Name,
Products = list_of_sql_data_record //this may be empty
}, commandType: System.Data.CommandType.StoredProcedure);