Dapper icon indicating copy to clipboard operation
Dapper copied to clipboard

Int32 to Bool mapping behaves differently for positional constructor than for property

Open TylerEich opened this issue 1 year ago • 3 comments

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

TylerEich avatar Sep 25 '23 19:09 TylerEich

Also have this problem. Frustrating having to create big long verbose DTOs instead of the succinct record syntax.

rathga avatar Nov 16 '23 09:11 rathga

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.

jokarls avatar Apr 10 '24 18:04 jokarls