vendredi 17 juillet 2015

How to close a sqlite file opened with System.Data.SQLite and DbContext?

In my EF6 recreation of this walkthrough for creating and accessing a SQLite database using System.Data.SQLite, I found that any access to the database leaves the file handle open for the database file long after I've disposed of the only disposable object I created: the DbContext.

This prevents me from deleting the database file when I'm done with it (e.g. at the conclusion of a unit test, or when the user of my app requests it).

You can see how simple my use of DbContext is and how File.Delete after closing it fails:

using (var context = new ChinookContext())
{
    var artists = from a in context.Artists
                    where a.Name.StartsWith("A")
                    orderby a.Name
                    select a;

    foreach (var artist in artists)
    {
        Console.WriteLine(artist.Name);
    }
}

// I've disposed of the DbContext. All handles to the sqlite database file SHOULD
// have been released by now.
// Yet, this next line fails because the file is still locked.
File.Delete("Chinook_Sqlite_AutoIncrementPKs.sqlite");

(full project context on github)

Any ideas of what I'm missing in order to close the file handle?

BTW, I'm aware of FAQ #22 on file locks only being released when I dispose of command, data reader, etc., but I haven't opened any of these myself so I don't know how I can dispose of them.

Aucun commentaire:

Enregistrer un commentaire