Commit cd751c21 authored by Hristo Stefanov's avatar Hristo Stefanov Committed by Nick Craver

Fixed issue with empty sqldatarecord list as a table valued parameter. (#900)

System.ArgumentException is thrown when passing empty SqlDataRecord list as an anonymous object or as table-valued parameter to Dapper. Added two unit tests.
parent 88348bd2
...@@ -241,6 +241,37 @@ public void TestTVPWithAnonymousObject() ...@@ -241,6 +241,37 @@ public void TestTVPWithAnonymousObject()
} }
} }
[Fact]
public void TestTVPWithAnonymousEmptyObject()
{
try
{
connection.Execute("CREATE TYPE int_list_type AS TABLE (n int NOT NULL PRIMARY KEY)");
connection.Execute("CREATE PROC get_ints @integers int_list_type READONLY AS select * from @integers");
var nums = connection.Query<int>("get_ints", new { integers = new IntCustomParam(new int[] { }) }, commandType: CommandType.StoredProcedure).ToList();
Assert.Equal(1, nums[0]);
Assert.Equal(2, nums[1]);
Assert.Equal(3, nums[2]);
Assert.Equal(3, nums.Count);
}
catch (ArgumentException ex)
{
Assert.True(string.Compare(ex.Message, "There are no records in the SqlDataRecord enumeration. To send a table-valued parameter with no rows, use a null reference for the value instead.") == 0);
}
finally
{
try
{
connection.Execute("DROP PROC get_ints");
}
finally
{
connection.Execute("DROP TYPE int_list_type");
}
}
}
// SQL Server specific test to demonstrate TVP // SQL Server specific test to demonstrate TVP
[Fact] [Fact]
public void TestTVP() public void TestTVP()
...@@ -367,6 +398,33 @@ public void TestSqlDataRecordListParametersWithAsTableValuedParameter() ...@@ -367,6 +398,33 @@ public void TestSqlDataRecordListParametersWithAsTableValuedParameter()
} }
} }
[Fact]
public void TestEmptySqlDataRecordListParametersWithAsTableValuedParameter()
{
try
{
connection.Execute("CREATE TYPE int_list_type AS TABLE (n int NOT NULL PRIMARY KEY)");
connection.Execute("CREATE PROC get_ints @integers int_list_type READONLY AS select * from @integers");
var emptyRecord = CreateSqlDataRecordList(Enumerable.Empty<int>());
var nums = connection.Query<int>("get_ints", new { integers = emptyRecord.AsTableValuedParameter() }, commandType: CommandType.StoredProcedure).ToList();
Assert.True(nums.Count == 0);
}
finally
{
try
{
connection.Execute("DROP PROC get_ints");
}
finally
{
connection.Execute("DROP TYPE int_list_type");
}
}
}
[Fact] [Fact]
public void TestSqlDataRecordListParametersWithTypeHandlers() public void TestSqlDataRecordListParametersWithTypeHandlers()
{ {
......
using System; using System.Collections.Generic;
using System.Collections.Generic;
using System.Data; using System.Data;
using System.Linq; using System.Linq;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment