Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mapping to tuple hides type incompatibilities #2108

Open
kristofturanszki opened this issue Aug 16, 2024 · 0 comments
Open

Mapping to tuple hides type incompatibilities #2108

kristofturanszki opened this issue Aug 16, 2024 · 0 comments

Comments

@kristofturanszki
Copy link

kristofturanszki commented Aug 16, 2024

Hi,
I was wondering if this is an intentional feature or a bug.

Correct behavior I think:
If one uses a designated type (for example a record) to map the result set and there is a type difference between the desired destination's field type and the result set, it throws a runtime error as expected.

Questionable behavior:
In case one uses a tuple for mapping a result set of a query/stored procedure, type inconsistencies are hidden, not like the first case.

Example code:

[Fact]
public async Task Independent()
{
    await using var connection = new SqlConnection("Data Source=localhost;Initial Catalog=test;TrustServerCertificate=True;Trusted_Connection=True");

    await connection.OpenAsync();

    //Tuple - No exception
    var result1 = await connection.QueryAsync<(long IntegerColumn, int BigintColumn)>("SELECT IntegerColumn, BigintColumn FROM dbo.TestTable");

    //With record - Correct mapping - No exception
    var result3 = await connection.QueryAsync<TestResultCorrect>("SELECT IntegerColumn, BigintColumn FROM dbo.TestTable");

    //With record - Incorrect mapping - Exception is thrown: A parameterless default constructor or one matching signature...
    var result2 = await connection.QueryAsync<TestResultIncorrect>("SELECT IntegerColumn, BigintColumn FROM dbo.TestTable");
}

private record TestResultIncorrect(long IntegerColumn, int BigintColumn);
private record TestResultCorrect(int IntegerColumn, long BigintColumn);

Datatable:

create table dbo.TestTable
(
    IntegerColumn int,
    BigintColumn  bigint
)
go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant