Skip to content

Commit 610e7f3

Browse files
committed
- fixed mapping when column does not exist
1 parent 78d3b2d commit 610e7f3

2 files changed

Lines changed: 44 additions & 7 deletions

File tree

Shuttle.Core.Data.Tests/QueryMapper/QueryMapperFixture.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,42 @@ select top 1
4242
Assert.AreEqual(2, mappedRows.Count());
4343
}
4444
}
45+
46+
[Test]
47+
public void Should_be_able_to_do_basic_mapping_even_though_columns_are_missing()
48+
{
49+
var mapper = new QueryMapper(new DatabaseGateway());
50+
51+
var queryRow = RawQuery.Create(@"
52+
select top 1
53+
Id,
54+
Name as NotMapped,
55+
Age as TheAge
56+
from
57+
BasicMapping
58+
");
59+
60+
var queryRows = RawQuery.Create(@"
61+
select
62+
Id,
63+
Name,
64+
Age
65+
from
66+
BasicMapping
67+
");
68+
69+
using (GetDatabaseContext())
70+
{
71+
var item = mapper.MapObject<BasicMapping>(queryRow);
72+
var items = mapper.MapObjects<BasicMapping>(queryRows);
73+
74+
Assert.AreEqual(2, items.Count());
75+
76+
var mappedRow = mapper.MapRow<BasicMapping>(queryRow);
77+
var mappedRows = mapper.MapRows<BasicMapping>(queryRows);
78+
79+
Assert.AreEqual(2, mappedRows.Count());
80+
}
81+
}
4582
}
4683
}

Shuttle.Core.Data/QueryMapper.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ public QueryMapper(IDatabaseGateway databaseGateway)
3131

3232
foreach (PropertyInfo pi in type.GetProperties())
3333
{
34-
var value = row[pi.Name];
35-
36-
if (value == null)
37-
{
38-
continue;
39-
}
40-
4134
try
4235
{
36+
var value = row.Table.Columns.Contains(pi.Name) ? row[pi.Name] : null;
37+
38+
if (value == null)
39+
{
40+
continue;
41+
}
42+
4343
pi.SetValue(result, value, null);
4444
}
4545
catch

0 commit comments

Comments
 (0)