View Ticket
Not logged in
Ticket UUID: 1ef72fac07013eb6654d5865764f08d21ba297ec
Title: EF6 keeps lock on a database file after context disposed
Status: Closed Type: Incident
Severity: Important Priority: Medium
Subsystem: LINQ Resolution: Rejected
Last Modified: 2016-08-25 00:34:44
Version Found In:
User Comments:
anonymous added on 2015-08-22 17:23:43:
When reading data from an SQLite database file using Entity Framework 6, the file keeps locked even after the context is disposed. I use the latest System.Data.SQLite package from Nuget.

using (var conn = new SQLiteConnection(@"Data Source=test.db3"))
using (var context = new DatabaseContext(conn))
	var value = context.TestTable.First().TestField;
File.Move(@"test.db3", @"test2.db3"); // System.IOException occurs there - the file is locked.

VS2015 solution that shows the problem:

mistachkin added on 2015-08-22 19:12:30:
This is most likely the same as an issue we saw before.  The Entity Framework
does not dispose of all IDisposable resources, which prevents them (and their
associated connection) from being cleaned up, hence the lock.

anonymous (claiming to be Eric Lloyd) added on 2016-08-16 21:12:56:
In my experience, closing the database connection directly doesn't solve the problem either. According to this StackOverflow answer [], System.Data.SQLite isn't actually releasing its hold on the file until a garbage-collection cycle finalizes the connection.

My code has to issue a GC.Collect() and GC.WaitForPendingFinalizers() to successfully release SQLite's locks on the database.

mistachkin added on 2016-08-25 00:34:44:
It's the Entity Framework that is not disposing of all its IDisposable database
resources that is the root cause of this problem.  Case closed.