1

I have a project that uses Microsoft.EntityFrameworkCore.Sqlite 6.0.5 on net6.0. I am expecting the Sqlite database file to be unlocked after the DbContext has been disposed.

However, the behaviour I am observing is that the Sqlite database remains locked after the DbContext has been disposed and finalised. There is a project that reproduces the behaviour here.

How can I unlock the database file?

My DbContext looks like this:

public class MyContext : DbContext
{
    ~MyContext()
    {
        Console.WriteLine("Finaliser was called.");
    }

    public override void Dispose()
    {
        base.Dispose();
        Console.WriteLine("Dispose was called.");
    }

    public static readonly string DbFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "_temp.db");

    public DbSet<Foo> Summaries { get; set; }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        builder.Entity<Foo>().HasKey(nameof(Foo.Id));
    }

    protected override void OnConfiguring(DbContextOptionsBuilder options)
    {
        options.UseSqlite($"Data Source={DbFile}");
    }
}

I am using it like this:

public static void AddItem()
{
    using var ctx = new MyContext();
    ctx.Database.EnsureCreated();
    ctx.Summaries.Add(new Foo {Bar = "Foo"});
    ctx.SaveChanges();        
}

1 Answer 1

3

Call SqliteConnection.ClearAllPools() directly or specify no pooling in connection string (Pooling=false)

https://learn.microsoft.com/en-us/dotnet/standard/data/sqlite/connection-strings#pooling

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.