2015-09-22 114 views
4

我试图使用实体框架6与SQLite和试图使用TransactionScope运行到数据库锁定问题。这是我的代码:TransactionScope和SQLite数据库被锁定

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,我也必须执行我打算加入到同一个交易(目前不存在)的文件系统操作。

回答

4

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

显然这个问题与MSDTC有关(TransactionScope与MSDTC紧密耦合)。

我发现a community addition to an MSDN page这反过来引用this blog post
......其中指出,交易是“提升”到MSDTC交易如果连接关闭并重新打开。哪些EF默认为。通常这是一件好事 - 你不希望数据库句柄永远悬挂 - 但在这种情况下,行为会阻碍。

解决的办法是明确地打开数据库连接:

using (var txn = new TransactionScope()) 
{ 
    using (var ctx = new CalibreContext()) 
    { 
     ctx.Connection.Open(); 
     // ... remainder as before ... 

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

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