Skip to content

Commit 42987a5

Browse files
authored
Merge pull request #1664 from DapperLib/craver/spliton-message
2 parents ffc02eb + 6af6252 commit 42987a5

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

Dapper/SqlMapper.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,7 +1679,7 @@ private static int GetNextSplitDynamic(int startIdx, string splitOn, IDataReader
16791679
{
16801680
if (startIdx == reader.FieldCount)
16811681
{
1682-
throw MultiMapException(reader);
1682+
throw MultiMapException(reader, splitOn);
16831683
}
16841684

16851685
if (splitOn == "*")
@@ -1713,7 +1713,7 @@ private static int GetNextSplit(int startIdx, string splitOn, IDataReader reader
17131713
}
17141714
}
17151715

1716-
throw MultiMapException(reader);
1716+
throw MultiMapException(reader, splitOn);
17171717
}
17181718

17191719
private static CacheInfo GetCacheInfo(Identity identity, object exampleParameters, bool addToCache)
@@ -1831,16 +1831,18 @@ private static Func<IDataReader, object> GetHandlerDeserializer(ITypeHandler han
18311831
return reader => handler.Parse(type, reader.GetValue(startBound));
18321832
}
18331833

1834-
private static Exception MultiMapException(IDataRecord reader)
1834+
private static Exception MultiMapException(IDataRecord reader, string splitOnColumnName = null)
18351835
{
18361836
bool hasFields = false;
18371837
try { hasFields = reader != null && reader.FieldCount != 0; }
18381838
catch { /* don't throw when trying to throw */ }
18391839
if (hasFields)
18401840
{
1841-
#pragma warning disable CA2208 // Instantiate argument exceptions correctly
1842-
return new ArgumentException("When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id", "splitOn");
1843-
#pragma warning restore CA2208 // Instantiate argument exceptions correctly
1841+
return new ArgumentException(
1842+
string.IsNullOrEmpty(splitOnColumnName)
1843+
? "When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id"
1844+
: $"Multi-map error: splitOn column '{splitOnColumnName}' was not found - please ensure your splitOn parameter is set and in the correct order",
1845+
"splitOn");
18441846
}
18451847
else
18461848
{

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Note: to get the latest pre-release build, add ` -Pre` to the end of the command
2727
- Parameters can now be re-used on subsequent commands (#952 via jamescrowley)
2828
- Array query support (`.Query<int[]>`) on supported platforms (e.g. Postgres) (#1598 via DarkWanderer)
2929
- `SqlMapper.HasTypeHandler` is made public for consumers (#1405 via brendangooden)
30+
- Improves multi-mapping error message when a specified column in splitOn can't be found (#1664 by NickCraver)
3031

3132
### 2.0.90
3233

tests/Dapper.Tests/MultiMapTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,13 @@ public void TestMultiMapperIsNotConfusedWithUnorderedCols()
194194
Assert.Equal("a", result.Item2.Name);
195195
}
196196

197+
[Fact]
198+
public void TestMultiMapperSplitOnError()
199+
{
200+
var ex = Assert.Throws<ArgumentException>(() => connection.Query<Foo1, Bar1, Tuple<Foo1, Bar1>>("select 1 as Id, 2 as BarId", Tuple.Create, splitOn: "DoesntExist").First());
201+
Assert.StartsWith("Multi-map error: splitOn column 'DoesntExist' was not found - please ensure your splitOn parameter is set and in the correct order", ex.Message);
202+
}
203+
197204
[Fact]
198205
public void TestMultiMapDynamic()
199206
{

0 commit comments

Comments
 (0)