Skip to content

Row version with ulong converter fails for optional navigation property without data when using projection #24446

Closed

Description

@billybraga

If I have a row version column in an optional relation, the NumberToBytesConverter fails when using projection to fetch the data when there is no child.

Repro repo

Basically, this:

public class Parent
{
    public Guid Id { get; set; }
    public OptionalChild? OptionalChild { get; set; }
}

public class OptionalChild
{
    public Guid Id { get; set; }
    public ICollection<Parent>? Parents { get; set; }
    public ulong Version { get; set; }
}

static async Task Main(string[] args)
{
    await using (var dbContext = AppDbContextFactory.Create(args))
    {
        await dbContext.AddAsync(
            new Parent()
            {
                Id = new("AF8451C3-61CB-4EDA-8282-92250D85EF03"),
            }
        );
        await dbContext.SaveChangesAsync();
    }

    await using (var dbContext = AppDbContextFactory.Create(args))
    {
        await dbContext
            .Parents
            .Select(
                x => new
                {
                    Child = new
                    {
                        Version = x.OptionalChild!.Version,
                    }
                }
            )
            .ToArrayAsync();
    }
}

Stack traces

Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at Microsoft.EntityFrameworkCore.Storage.ValueConversion.NumberToBytesConverter`1.ReverseLong(Byte[] bytes)
   at lambda_method13(Closure , QueryContext , DbDataReader , ResultContext , SingleQueryResultCoordinator )
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToArrayAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)

Verbose output

dbug: 2021-03-19 09:03:26.913 CoreEventId.QueryCompilationStarting[10111] (Microsoft.EntityFrameworkCore.Query) 
      Compiling query expression: 
      'DbSet<Parent>()
          .Select(x => new { 
              Id = x.Id, 
              Child = new { 
                  Id = x.OptionalChild.Id, 
                  Version = x.OptionalChild.Version
               }
           })'
dbug: 2021-03-19 09:03:27.291 CoreEventId.QueryExecutionPlanned[10107] (Microsoft.EntityFrameworkCore.Query) 
      Generated query execution expression: 
      'queryContext => new SingleQueryingEnumerable<<>f__AnonymousType3<Guid, <>f__AnonymousType0<Guid, ulong>>>(
          (RelationalQueryContext)queryContext, 
          RelationalCommandCache.SelectExpression(
              Projection Mapping:
                  Id -> 0
                  Child.Id -> 1
                  Child.Version -> 2
              SELECT p.Id, o.Id, o.Version
              FROM Parents AS p
              LEFT JOIN OptionalChildren AS o ON p.OptionalChildId == o.Id), 
          Func<QueryContext, DbDataReader, ResultContext, SingleQueryResultCoordinator, <>f__AnonymousType3<Guid, <>f__AnonymousType0<Guid, ulong>>>, 
          EfRowNumberBug.AppDbContext, 
          False, 
          False
      )'
dbug: 2021-03-19 09:03:27.348 RelationalEventId.CommandCreating[20103] (Microsoft.EntityFrameworkCore.Database.Command) 
      Creating DbCommand for 'ExecuteReader'.
dbug: 2021-03-19 09:03:27.349 RelationalEventId.CommandCreated[20104] (Microsoft.EntityFrameworkCore.Database.Command) 
      Created DbCommand for 'ExecuteReader' (0ms).
dbug: 2021-03-19 09:03:27.349 RelationalEventId.ConnectionOpening[20000] (Microsoft.EntityFrameworkCore.Database.Connection) 
      Opening connection to database 'ef-row-number-bug-test-db' on server '127.0.0.1,1433'.
dbug: 2021-03-19 09:03:27.349 RelationalEventId.ConnectionOpened[20001] (Microsoft.EntityFrameworkCore.Database.Connection) 
      Opened connection to database 'ef-row-number-bug-test-db' on server '127.0.0.1,1433'.
dbug: 2021-03-19 09:03:27.349 RelationalEventId.CommandExecuting[20100] (Microsoft.EntityFrameworkCore.Database.Command) 
      Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [p].[Id], [o].[Id], [o].[Version]
      FROM [Parents] AS [p]
      LEFT JOIN [OptionalChildren] AS [o] ON [p].[OptionalChildId] = [o].[Id]
info: 2021-03-19 09:03:27.351 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command) 
      Executed DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [p].[Id], [o].[Id], [o].[Version]
      FROM [Parents] AS [p]
      LEFT JOIN [OptionalChildren] AS [o] ON [p].[OptionalChildId] = [o].[Id]
fail: 2021-03-19 09:03:27.389 CoreEventId.QueryIterationFailed[10100] (Microsoft.EntityFrameworkCore.Query) 
      An exception occurred while iterating over the results of a query for context type 'EfRowNumberBug.AppDbContext'.
      System.IndexOutOfRangeException: Index was outside the bounds of the array.
         at Microsoft.EntityFrameworkCore.Storage.ValueConversion.NumberToBytesConverter`1.ReverseLong(Byte[] bytes)
         at lambda_method13(Closure , QueryContext , DbDataReader , ResultContext , SingleQueryResultCoordinator )
         at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()

EF Core version: 5.0.4
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 5..
Operating system: Ubuntu 20.04

Activity

  1. changed the title [-]Row version with ulong converter fails for optional navigation property without data[/-] [+]Row version with ulong converter fails for optional navigation property without data when using projection[/+] on Mar 19, 2021
  2. self-assigned this
    on Mar 23, 2021
  3. added this to the 6.0.0 milestone on Mar 23, 2021
  4. added 2 commits that reference this issue on Mar 25, 2021
    f44cff5
    3cffeb1
  5. modified the milestones: 6.0.0, on Mar 25, 2021
  6. added a commit that references this issue on Mar 27, 2021
    786d60c
  7. modified the milestones: , 6.0.0 on Nov 8, 2021
  8. removed their assignment
    on Sep 1, 2024
  9. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions