Dapper
Dapper copied to clipboard
Int32 to Bool mapping behaves differently for positional constructor than for property
This may or may not be a bug but the behavior seemed interesting, so I thought I'd ask.
I isolated this test case, where two identical query strings fill objects with the same shape but different constructors. The one mapped by constructor does not work, but the one mapped by property does work.
private record InConstructor(bool B);
[Fact] // This one will fail
public void TestFillInConstructor()
{
InConstructor ctor = connection.Query<InConstructor>("select 1 B").First();
Assert.True(ctor.B);
}
private record InProperty
{
public bool B { get; init; }
}
[Fact] // This one will succeed
public void TestFillInProperty()
{
InProperty prop = connection.Query<InProperty>("select 1 B").First();
Assert.True(prop.B);
}
It looks like the constructor version is looking for an Int32
in the position where Bool
exists, since the returned type from the SQL is an int
Also have this problem. Frustrating having to create big long verbose DTOs instead of the succinct record syntax.
I have the same problem where the database returns a double
, but the type I'm mapping it to is a string
. It works when mapping is done via the props, but not using the constructor.