TransactionScope和SQLite数据库被锁定

Men*_*sur 6 sqlite transactionscope entity-framework-6

我试图在SQLite中使用Entity Framework 6并在尝试使用时遇到数据库锁定问题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();
}
Run Code Online (Sandbox Code Playgroud)

第一行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>
Run Code Online (Sandbox Code Playgroud)

我需要使用,TransactionScope因为除了执行数据库操作,我还必须执行我计划添加到同一事务(目前不存在)的文件系统操作.

Dav*_*vid 5

我遇到了类似的问题。为了清楚起见,我在第二个查询中遇到的错误是“底层提供程序在打开时失败”(但打开失败的原因是数据库被锁定)。

显然,该问题与 MSDTC(与 MSDTCTransactionScope紧密耦合)有关。

我在MSDN 页面上找到了一个社区补充,该页面又引用了这篇博客文章
......其中指出,如果连接关闭并重新打开,事务将“提升”为 MSDTC 事务。默认情况下 EF 会这样做。通常这是一件好事——你不希望数据库句柄永远挂在那里——但在这种情况下,行为会妨碍你。

解决方法是显式打开数据库连接:

using (var txn = new TransactionScope())
{
    using (var ctx = new CalibreContext())
    {
        ctx.Connection.Open();
        // ... remainder as before ...
Run Code Online (Sandbox Code Playgroud)

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

这似乎解决了我的问题。如果我还有什么要报告的,我会发布更新。