I got following exception when I tried to modify the SQLite database using Entity Framework
System.Data.Entity.Core.EntityException: System.Data.Entity.Core.EntityException: An error occurred while starting a transaction on the provider connection. See the inner exception for details. ---> System.Data.SQLite.SQLiteException: database is locked
database is locked.
The code causing the problem is following (the orderDao and productDao is wrapper of Entity Framework to manipulate SQLite database)
1 2 3 4 5 6 7 8 9 10 11 12 13 |
orderDao.Add(new Order() { CreateTime = DateTime.Now, OrderId = DateTime.Now.ToString("yyyyMMddHHmmss"), ItemDetails = new List<ProductPurchase>() { new ProductPurchase() { Amount = 2, Product = productDao.Find(p => p.Title == "Milk").FirstOrDefault() } } }); |
One common situation to cause this problem is that another application is accessing the same database.
In my situation it's because that I opened the database with DB Browser for SQLite, deleted a database table and not applying the changes.
Clicking Write Changes (or Revert Changes or Close Database) and the error will be gone.