3

I'm using SQLite for .Net and EntityFramework from .Net Framework 4.0. Everything was ok until I moved from SQLite v.1.0.79.0 to v.1.0.86.0. From that time code like this

using (var context = new EntityContent(sqliteConnectionString))
{
    context.ExecuteStoreCommand(mySqlScriptString);
    /*
     * or do some stuff with context entities and call context.SaveChanges()
     */
}
deleteSQLiteDbFile(getDbFilePath(sqliteConnectionString)); // <-- here is the error

throws the error System.IO.IOException: "The process cannot access the file 'mydatabase.dat' because it is being used by another process."

It's all because of moving to the new sqlite3_close_v2() API in SQLite v.1.0.82.0 (see this). As you can see in this ticket you should properly dispose all sqlite commands and readers. But EF 4 doesn't do it (at least in the .net framework 4 version). For instance, see ObjectContent.ExecuteStoreCommand method. It implicitly creates a command from SQLite connection and doesn't dispose it.

Yes, I can write my own executeStoreCommand(ObjectContent context, string script, params object[] parameters) method with proper command disposing but what to do with context.SaveChanges()?

Does anybody know if it fixed in next releases of EF or SQLite? Or may be there are any workarounds?

sedovav
  • 1,986
  • 1
  • 17
  • 28

1 Answers1

0

The reason for this problem is a changed close behavior in the System.Data.SQLite which is somehow not compatible with EF anymore. Details see here: http://www.mail-archive.com/sqlite-users%40sqlite.org/msg74770.html

Since this was only in my integration tests I used the workaround from here to solve that: System.Data.SQLite Close() not releasing database file

Community
  • 1
  • 1
Harry13
  • 733
  • 1
  • 4
  • 15