Skip to content

Incorrect IsDBNull results on null rowversion when using ReadAsync #1228

@roji

Description

@roji

When calling IsDBNull on a rowversion column after having called ReadAsync, false is return for a null value instead of true. This occurs on Microsoft.Data.SqlClient 3.0, and is likely related to #998 (/cc @Wraith2).

The below minimal repro uses a left join to produce a null rowversion. This was originally raised and repro'd on EF Core by @frankbuckley in dotnet/efcore#25074, thanks.

Repro
await using var conn = new SqlConnection("Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0");
await conn.OpenAsync();

await using var cmd = new SqlCommand(@"
DROP TABLE IF EXISTS [Price];
DROP TABLE IF EXISTS [Occurrences];

CREATE TABLE [Occurrences] (
    [Id] int NOT NULL IDENTITY,
    CONSTRAINT [PK_Occurrences] PRIMARY KEY ([Id])
);

CREATE TABLE [Price] (
    [Id] int NOT NULL IDENTITY,
    [Timestamp] rowversion NOT NULL,
    [OccurrenceId] int NOT NULL,
    [Value] decimal(18,2) NOT NULL,
    CONSTRAINT [PK_Price] PRIMARY KEY ([Id]),
    CONSTRAINT [FK_Price_Occurrences_OccurrenceId] FOREIGN KEY ([OccurrenceId]) REFERENCES [Occurrences] ([Id]) ON DELETE CASCADE
);

INSERT INTO [Occurrences] DEFAULT VALUES;
INSERT INTO [Occurrences] DEFAULT VALUES;", conn);

await cmd.ExecuteNonQueryAsync();

cmd.CommandText = @"
SELECT [o].[Id], [p].[Id], [p].[OccurrenceId], [p].[Timestamp], [p].[Value]
FROM [Occurrences] AS [o]
LEFT JOIN [Price] AS [p] ON [o].[Id] = [p].[OccurrenceId]
ORDER BY [o].[Id]";

await using var reader = await cmd.ExecuteReaderAsync();

// Read one row with Read - works fine
reader.Read();
Console.WriteLine($"IsDBNull after Read: {reader.IsDBNull(3)}");

// Read another row with ReadAsync - bug
await reader.ReadAsync();
Console.WriteLine($"IsDBNull after ReadAsync: {reader.IsDBNull(3)}");
_ = reader.GetFieldValue<byte[]>(3); // Throws

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions