Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework with SQLite Error: An error occurred while reading from the store provider's data reader

I'm using SQLite 1.0.89 with EF 5 .NET Framework 4.5 on VS2013 in a WPF application in C#. The DB size is not big and the table that the program use contain max 1000 row.

using the program I found this error often:

An error occurred while reading from the store provider's data reader. See the inner exception for details.

the inner exception is :

{"library routine called out of sequence\r\nnot an error"}

Other time the inner exception is:

Connection was closed, statement was terminated

Another time i found:

unknown error\r\nno connection handle available

I found this article searching:

Parallel.Foreach loop creating multiple db connections throws connection errors?

SQL Server CE database size issue

but no one solve my problem.

The query that the program do IS NOT inside a loop, but are single query performed when button is pressed on the UI but i noticed that the error happens more often (bun not only) when I press the query button a lot of time faster.

Other thing. The error happens more often (but again not only) when the DB Context is access via a properties istead of a method example:

public List<Product> ProductList
{
get {
      return DBContext.Products.ToList();
    }
}
like image 376
abrfra Avatar asked Feb 12 '23 16:02

abrfra


1 Answers

The problem was caused by multiple thread that query the db using the same DBContext.

Using a different DBContext for each thread solve the problem.

In my case I was using Castle Windsor to inject the DBContext into the class that perform the query. Configuring the lifestyle of the DBContext to one per thread the problem has gone.

like image 155
abrfra Avatar answered Feb 15 '23 07:02

abrfra