gpt4 book ai didi

sqlite - TransactionScope 和 SQLite 数据库被锁定

转载 作者:IT王子 更新时间:2023-10-29 06:26:11 26 4
gpt4 key购买 nike

我正在尝试将 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,因为除了执行数据库操作外,我还必须执行我计划添加到同一事务(目前不存在)的文件系统操作。

最佳答案

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

显然这个问题与 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 构造函数中打开连接。

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

关于sqlite - TransactionScope 和 SQLite 数据库被锁定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32721283/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com