SQLite Forum

EntityFramework database is locked

EntityFramework database is locked

(1) By Horacio (htorres) on 2020-07-26 04:52:16 [link] [source]

Im' writing a ASP.NET WebForms app on .Net Framework 4.7.2 EntityFramework 6.4.4 System.Data.SQLite

When I try to execute a transaction, error below is raised.

        using (var context = new Entities())
            using (var ts = context.Database.BeginTransaction())
             .....do something


An error occurred while starting a transaction on the provider connection database is locked

This is my connection string. <add name="DefaultConnection" connectionString="Data Source=|DataDirectory|Mydb.db;Version=3;Pooling=True;Max Pool Size=100;" providerName="System.Data.SQLite.EF6" />

I'll appreciate your help

(2) By Clemens Ladisch (cladisch) on 2020-07-28 07:28:22 in reply to 1 [source]

Some other connection has an active transaction on the database.

To allow waiting for other transactions to finish, you should set the busy timeout on each connection. If this is not possible in the connection string, then the correct place is the connection's StateChange event:

Connection.StateChange += ConnectionStateChange;

void ConnectionStateChange(object sender, StateChangeEventArgs e)
    if (e.CurrentState == ConnectionState.Open)
         db.ExecuteStoreCommand("PRAGMA busy_timeout = 12345");

But if that other code has a bug an never ends its transaction, then no amount of waiting will help.