Skip to content

Error "Index was outside the bounds of the array." when reading from dataset without AsNoTracking() #30764

@luizfbicalho

Description

@luizfbicalho

Ask a question

I'm updating a model from EF core 5.0 to 7.0, and I have one DbContextHealthCheck that reads all datasets and reads 10 objects just to ensure that everything is working.

For one entity I'm getting the Index was outside the bounds of the array. Exception

If I use the AsNoTracking() in the query it runs fine, but I didn't find any reason for the error
I created one small project with similar entities but it works fine.

My real question is:

How can I see or override this code to inspect more what's going on.
The problem is inside the InternalEntityEntry Constructor and in the SnapshotFactoryFactory class

 public class DbContextsHealthCheck : IHealthCheck
    {
        public DbContextsHealthCheck(IEnumerable<DbContext> contexts)
        {
            Contexts = contexts;

        }

        public IEnumerable<DbContext> Contexts { get; }

        public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
        {
            foreach (var db in Contexts)
            {
                try
                {
                    await db.Database.MigrateAsync(cancellationToken);
                    var prop = db.GetType().GetProperties().Where(x => typeof(IQueryable).IsAssignableFrom(x.PropertyType));
                    foreach (var set in prop)
                    {
                        var dbset = set.GetValue(db);
                        if (dbset is IQueryable enumerable)
                        {
                            try
                            {
                                await enumerable.OfType<object>().Take(10).ToListAsync(cancellationToken);
                            }
                            catch (Exception ex)
                            {
                                return await Task.FromResult(HealthCheckResult.Degraded($"The Database {db.Database.GetDbConnection().ConnectionString}  and property {set.Name} is degraded ", ex));
                            }

                        }
                    }
                }
                catch (Exception ex)
                {
                    return await Task.FromResult(HealthCheckResult.Unhealthy($"The Database {db.Database.GetDbConnection().ConnectionString} is unhealthy ", ex));
                }
            }

            return await Task.FromResult(HealthCheckResult.Healthy("The Database is healthy"));
        }
    }

Include stack traces

   at lambda_method339(Closure, ValueBuffer)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry..ctor(IStateManager stateManager, IEntityType entityType, Object entity, ValueBuffer& valueBuffer)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.StartTrackingFromQuery(IEntityType baseEntityType, Object entity, ValueBuffer& valueBuffer)
   at Microsoft.EntityFrameworkCore.Query.QueryContext.StartTracking(IEntityType entityType, Object entity, ValueBuffer valueBuffer)
   at lambda_method1140(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 MC.Webapi.Extensions.HealthCheck.DbContextsHealthCheck.CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken)",

Include provider and version information

EF Core version: 7.0.5
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 7.0
Operating system: Windows 10
IDE: Visual Studio 2022 17.6 Preview 5

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions