question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

System InvalidOperationException ("Invalid attempt to call NextResult when reader is closed.") in Microsoft.Data.Sqlite

See original GitHub issue

When using EF core 3.0 and calling some of the DbContext methods there is a System InvalidOperation exception thrown. If I change the exception settings in VS to stop on this type of exception the description is “Invalid attempt to call NextResult when reader is closed.” For the application i am working on the exception is thrown many times in a tight loop which slows down VS considerably. When running my application outside VS it works as expected so the exception must be handled inside EF core somewhere.

This could be related to issue https://github.com/aspnet/EntityFrameworkCore/issues/17614#issuecomment-540163786 which is where I got the code to reproduce the issue.

The methods causing the exception are: context.Collection.ToList(); context.SaveChanges(); dbContext.MyObject.Find(ID); <- not able to repo this one in a simple project but is happening in the codebase I am working on.

To Reproduce

The attached simple console app reproduces the issue.

using System.ComponentModel.DataAnnotations;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using System.Linq;

namespace EFCore_Test
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var context = new MyDbContext())
            {
                context.Database.EnsureCreated();

                var result = context.Collection.ToList();   // Throws exception.
                context.Collection.Add(new MyObject());
                context.SaveChanges();                      // Throws exception.
                result = context.Collection.ToList();       // Throws exception.

            }
        }
    }

    class MyDbContext : DbContext
    {
        public DbSet<MyObject> Collection { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            var connection = new SqliteConnection("Data Source=dbFile.Db");
            connection.Open();
            optionsBuilder.UseSqlite(connection);
        }
    }

    class MyObject
    {
        public int Id { get; set; }
    }

}

Additional context

Microsoft.Data.Sqlite version: 3.0.0 Target framework: NET Core 3.0 Operating system: Win 10 1903 EF Core 3.0.0 Visual Studio version 16.3.2

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
rojicommented, Oct 24, 2019

Looked into this as part of #18509, and reproduced the issue.

When Dispose or Closed is called on the same SqliteDataReader more than once, the dispose logic is repeated. Part of that logic is to execute NextResult, which throws an exception is the reader has already been closed/disposed. That exception is caught and ignored in Dispose, but it does cause a needless perf slowdown and disrupts the debugging experience.

Submitted #18572 to exit Dispose early if the reader has already been closed/disposed.

0reactions
rojicommented, Oct 24, 2019

PS not sure why the issue disappeared for users above, but it’s likely to be related to VS exception setting changes across versions. Confirmed that with regular EF Core code (e.g. ctx.Blogs.ToList()) an exception is generated internally before #18572.

Read more comments on GitHub >

github_iconTop Results From Across the Web

invalid attempt to call read when READER IS CLOSED
I suspect that something is happening in code that you do not show here. You can do something (for diagnostic purposes at least)...
Read more >
C# Invalid attempt to call Read when reader is closed
It doesn't work because you close the connection before returning the reader. Reader works only when the connection is open:
Read more >
sqldatareader error "Invalid attempt to call Read when ...
Hello there, when I am reading data from sqldatareader I am getting error "Invalid attempt to call Read when reader is closed." My...
Read more >
Invalid attempt to call IsDBNull when reader is closed exception
A "Invalid attempt to call IsDBNull when reader is closed" exception is thrown at various places in the code. Steps to Reproduce.
Read more >
Solve error invalid attempt to read when no data is present in ...
NET(VB.net): load data from database into gridview and get vaules ... be closed first. invalid attempt to call read when reader is closed...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found