Every query on SQLite DB using EF Core in Docker is very slow, unless there is an explicit call to OpenConnection Every query on SQLite DB using EF Core in Docker is very slow, unless there is an explicit call to OpenConnection docker docker

Every query on SQLite DB using EF Core in Docker is very slow, unless there is an explicit call to OpenConnection


EF Core 3.x included one breaking change that affects this use-case, the connection is closed earlier:

Database connection is now closed if not used anymore before theTransactionScope has been completed

Mitigations:

If the connection needs to remain open explicit call toOpenConnection() will ensure that EF Core doesn't close itprematurely

The sqlite provider is still not using connection pooling: 13837;

With the new releases of EntityFrameworkCore.Sqlite (>3.0), there are some new optimizations regarding transactionality, and new .WAL file is created when the .db file is open; This, combined with lack of connection pooling results in creating and removing the .WAL file with every request; And docker volume/file mapping to windows file system gets to be very time consuming.


There is not much more to explain than what Lucian has already done in the accepted answer. Anyway, I will restate to contextualize the link.

The problem is all about EF Core closing and reopening the connection every statement.

One way to work around this is providing the connection (already open) by yourself. EF does not auto close connections that were already open.

As he also stated, at the time of this answer, the connection provider for SQLite still don't use a connection pool.

The best way to achieve the maximum performance with EF Core and SQLite is to provide a connection pool. The goal of the pool is maintaining some connections always open, because the problem itself is the overhead of opening new connections.

Finally, the link is a comment that I've made in the EF Core GitHub issue tracker containing a complete example of how to do that in a simple way.

https://github.com/dotnet/efcore/issues/13837#issuecomment-821717602