locked
IndexOutOfRangeException error when reading all rows from table in EF Core 2.0 RRS feed

  • Question

  • User-1142747527 posted

    I have a Web API targetting .Net Core 2.0. The data source is a MySQL database, and I'm using Pomelo.EntityFrameworkCore.MySql 2.0.0.1.

    I can load data from all the tables in my database except for one. I don't know why, but when I try to load all records in this database, by executing a simple:

    public class MyContext : DbContext
    {
        public DbSet<Condition> Condition { get; set; }
    }
    
    public class MyController : Controller
    {
        private readonly MyContext _context;
     
        // GET: api/Sync
    [HttpGet]
    public async Task<IActionResult> GetChangeSet() { List<Condition> conditions = _context.Condition.ToList(); } }

    I get the following error:

        _context.Condition.ToList()' threw an exception of type 'System.IndexOutOfRangeException'

        Data: {System.Collections.ListDictionaryInternal}
        HResult: -2146233080
        HelpLink: null
        InnerException: null
        Message: "Index was outside the bounds of the array."
        Source: "Anonymously Hosted DynamicMethods Assembly"
        StackTrace: "   at lambda_method(Closure , ValueBuffer )\r\n   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalMixedEntityEntry..ctor(IStateManager stateManager, IEntityType entityType, Object entity, ValueBuffer valueBuffer)\r\n   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryFactory.NewInternalEntityEntry(IStateManager stateManager, IEntityType entityType, Object entity, ValueBuffer valueBuffer)\r\n   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryFactory.Create(IStateManager stateManager, IEntityType entityType, Object entity, ValueBuffer valueBuffer)\r\n   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.StartTrackingFromQuery(IEntityType baseEntityType, Object entity, ValueBuffer valueBuffer, ISet`1 handledForeignKeys)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.EntityTrackingInfo.StartTracking(IStateManager stateManager, Object entity, ValueBuffer valueBuffer)\r\n   at Microsoft.EntityFrameworkC
    ore.Query.Internal.QueryBuffer.StartTracking(Object entity, EntityTrackingInfo entityTrackingInfo)\r\n   at Microsoft.EntityFrameworkCore.Query.QueryContext.StartTracking(Object entity, EntityTrackingInfo entityTrackingInfo)\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.<_TrackEntities>d__17`2.MoveNext()\r\n   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()\r\n   at System.Collections.Generic.List`1.AddEnumerable(IEnumerable`1 enumerable)\r\n   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)"
        TargetSite: {Microsoft.EntityFrameworkCore.ChangeTracking.Internal.ISnapshot lambda_method(System.Runtime.CompilerServices.Closure, Microsoft.EntityFrameworkCore.Storage.ValueBuffer)}

    This is what my Condition model looks like:

    namespace Inspections.Data.Models
    {
        [Table("condition")]
        public class Condition
        {
            [Key]
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            [Column("condition_id")]
            public int Id { get; set; }
    
            [Column("condition_name")]
            public string ConditionName { get; set; }
    
            [Column("group_id")]
            public int? GroupId { get; set; }
            [ForeignKey("group_id")]
            public virtual Group Group { get; set; }
    
            [Column("added_by_id")]
            public int? AddedById { get; set; }
            [ForeignKey("AddedById")]
            public virtual User AddedBy { get; set; }
    
            [Column("added_date")]
            public DateTime AddedDate { get; set; }
    
            [Column("deleted_by_id")]
            public int? DeletedById { get; set; }
            [ForeignKey("DeletedById")]
            public virtual User DeletedBy { get; set; }
    
            [Column("deleted_date")]
            public DateTime? DeletedDate { get; set; }
    
            [Column("modified_date")]
            public DateTime? ModifiedDate { get; set; }
        }
    }
    

    The exact same thing works perfectly when reading from all the other tables in the database. And the table is populated with a number of rows...

    Any ideas?

    Thursday, November 2, 2017 10:36 AM

Answers

  • User-1142747527 posted

    Sorry, found the problem. Was just a typo :/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 2, 2017 10:42 AM