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

SqlResult.DynamicParameters loses type information for nulls #726

Open
shokurov opened this issue Sep 15, 2024 · 0 comments
Open

SqlResult.DynamicParameters loses type information for nulls #726

shokurov opened this issue Sep 15, 2024 · 0 comments

Comments

@shokurov
Copy link

Here is the simple example showing the problem (even though it does not call SqlKata):

[Fact]
    public void Dapper_WithSqlServerFailsWithRegularBinaryNullable()
    {
        // Arrange
        using var connection = new SqlConnection(Fixture.ConnectionString);
        connection.Open();
        // This simulates what SqlKata Compiler does with typed parameters: it converts them into dictionary
        // SqlResult.NamedBindings thus losing the type information for null parameters
        var parametersObject = new Dictionary<string, object?>
        {
            // ReSharper disable once RedundantCast
            ["BinaryData"] = (byte[]?)null
        };
        // Act
        var action = () =>
            // ReSharper disable once AccessToDisposedClosure
            connection.Execute(
                "INSERT INTO NullableVarbinary (BinaryData) VALUES (@BinaryData)",
                parametersObject
            );
        // Assert
        action
            .Should()
            .Throw<SqlException>()
            .WithMessage(
                "Implicit conversion from data type nvarchar to varbinary(max) is not allowed. Use the CONVERT function to run this query."
            );
    }

The way the SqlResult.DynamicParameters are declared makes it impossible to pass null value while maintaining type information for Dapper to do its job mapping to DB type.

Since the class is declared in core library and used by all compilers, it's hard to work around this problem.

I would propose making NamedBindings an interface similar to Dapper.IDynamicParameters with implementation that could be replaced for a QueryFactory.

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