事务范围和SQLite数据库被锁定问题

8

我正在尝试使用Entity Framework 6与SQLite,并在尝试使用TransactionScope时遇到了数据库锁定问题。以下是我的代码:

using (var txn = new TransactionScope())
{
    using (var ctx = new CalibreContext())
    {
        var book = ctx.Books.First(x => x.Id == 2);
        var author = ctx.Authors.First(x => x.Id == 3);
        book.Authors.Add(author);
        ctx.SaveChanges();
    }
    txn.Complete();
}

第一行var book = ctx.Books.First(x => x.Id == 2);执行正常。但是当我继续执行下一个操作时,会出现一个异常,提示我的数据库被锁定了。这是我的应用程序配置:

<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
  </startup>
  <connectionStrings>
    <add name="CalibreContext" connectionString="Data Source=metadata.db" providerName="System.Data.SQLite.EF6" />
  </connectionStrings>
  <system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SQLite" />
      <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite, Version=1.0.98.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
      <remove invariant="System.Data.SQLite.EF6" />
      <add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".Net Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6, Version=1.0.98.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
    </DbProviderFactories>
  </system.data>
  <entityFramework>
    <defaultConnectionFactory type="Calibre.Dal.Ef.SQLiteConnectionFactory, Calibre.Dal.Ef" />
    <providers>
      <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6, Version=1.0.98.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
      <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6, Version=1.0.98.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
    </providers>
  </entityFramework>
</configuration>

我需要使用TransactionScope,因为除了执行数据库操作外,我还需要执行一个文件系统操作,并计划将其添加到同一事务中(当前不存在)。
2个回答

6
我遇到了类似的问题。仅仅为了明确,我在第二个查询中得到的错误是“底层提供程序在打开时失败”(但打开失败的原因是数据库被锁定)。
显然,这个问题与MSDTC有关(TransactionScope与MSDTC紧密耦合)。
我在MSDN页面社区附加内容中找到了一个参考这篇博客文章...该文章指出,如果关闭并重新打开连接,则事务将“提升”为MSDTC事务。EF默认情况下会这样做。通常这是件好事--你不想让数据库句柄永远挂着--但在这种情况下,这种行为会妨碍进展。
解决方法是显式打开数据库连接:
using (var txn = new TransactionScope())
{
    using (var ctx = new CalibreContext())
    {
        ctx.Connection.Open();
        // ... remainder as before ...

或者,如果您所有的CalibreContext对象都是短暂存在的,您可以在CalibreContext构造函数中打开连接。

这似乎解决了我的问题。如果有其他需要报告的事情,我会发布更新。


不确定你使用的是哪个 EF 版本,但对我来说 ctx 没有 Connection 属性。 - emilaz
@emilaz,这是EF6.x版本 - 也就是说,根据原始帖子上的标签,它是在 .NET Core / EF Core 之前的。 - David

1

导致这个问题的常见情况是另一个应用程序正在访问相同的数据库。

在我的情况下,原因是我使用 DB Browser for SQLite 打开了数据库,删除了一个数据库表但没有应用更改。

点击“写入更改”(或“撤消更改”或“关闭数据库”),错误就会消失。

(摘自 http://redino.net/blog/2017/03/net-sqlite-database-locked


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接