我有一個專案使用Microsoft.EntityFrameworkCore.Sqlite 6.0.5. net6.0我希望在處理完Sqlite資料庫檔案后解鎖資料庫檔案DbContext。
但是,我觀察到的行為是資料庫在處理和完成Sqlite后仍處于鎖定狀態。DbContext有一個專案可以重現這里的行為。
如何解鎖資料庫檔案?
我的DbContext樣子是這樣的:
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}");
}
}
我這樣使用它:
public static void AddItem()
{
using var ctx = new MyContext();
ctx.Database.EnsureCreated();
ctx.Summaries.Add(new Foo {Bar = "Foo"});
ctx.SaveChanges();
}
uj5u.com熱心網友回復:
ClearAllPools() 或在連接字串中不指定池 (Pooling=false)
https://docs.microsoft.com/en-us/dotnet/standard/data/sqlite/connection-strings#pooling
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/490808.html
上一篇:SQLite查詢/子查詢的問題
