- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在尝试将 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/
我有一个应用程序,我想将多个数据库保存到一个事务中。如果他们中的任何一个失败了,我想把整个事情都推回去。但是,我想在回滚事务之前知道其中哪些失败(或成功)。 我有一个带有内部循环的外部 Transac
我刚刚开始玩 Dapper。到目前为止我很喜欢它。 dapper 不能与 TransactionScope 一起使用吗?我注意到,即使我从不调用 TransactionScope.Complete,我
您好首先感谢您对这个问题的关注;有没有办法在c#中实现这样的事务 using (Transactionscope x=new Transactionscope ()) { Thead A()=>
这是使用事务范围的正确方法吗: 我有一个代表事物的一部分的对象: public class ThingPart { private DbProviderFactory connectionFa
我正在尝试找到在使用 NHibernate 的 Web 应用程序中处理事务的最佳解决方案。 我们使用 IHttpModule 并在 HttpApplication.BeginRequest 我们打开一
我面临以下问题: 在我的项目中,我在应用程序正在使用的同一个数据库中进行了错误登录。这意味着,如果发生错误,那么在每个 catch 中,都会将错误存储到 DB 中。 然而,问题是在使用事务时。发生错误
我想知道在处理多线程时如何正确使用 TransactionScope 类? 我们在主线程中创建一个新的作用域,然后生成几个工作线程,我们希望它们参与主作用域,因此,如果作用域从未完成,则在每个工作线程
任何人都可以给我一个关于使用 TransactionScope 和 NHibernate 的快速概述吗?我需要对 session /IEnlistmentNotification/等做一些特别的事情吗
如果我执行以下操作: Using scope = New TransactionScope() entries.Content.ReadAs(Of IList(Of WebMaint
当 TransactionScope 首次推出时,我在让它在我的开发机器 (XP) 和我们的数据库服务器 (Windows Server 2003) 之间工作时遇到了一些严重的问题。 当我进一步研究时
我在基于 Silverlight 和 RIA 服务的项目中使用 TransactionScope 类。每次我需要保存一些数据时,我都会创建一个 TransactionScope 对象,使用 Oracl
TransactionScope 是否适用于封闭的数据库连接? using (var transaction = new TransactionScope(TransactionScopeOption
我有两个 PL/SQL 存储过程,每个过程都处理自己的事务(开始/提交和发生错误时的回滚)。从 .Net 代码我调用这两个 SP,如下所示。 using (TransactionScope ts
我在我的应用程序中使用了 TransactionScope 类,因为我需要以交互方式、自然地执行一些操作(要么全部成功,要么全都不成功)。 由于此操作不仅与数据库相关,我通过实现 IEnlistmen
您好,有没有办法检查使用 TransactionScope 的事务是提交还是回滚? 最佳答案 在处理 TransactionScope 之前,实际的提交不会发生。如果 Commit 由于任何原因失败,
我有一个 Web 应用程序,许多用户在任何给定时刻都在使用它。从数据库的角度来看,他们在系统中移动时不断地从任意数量的表中写入和读取数据。此外,我们还有许多将数据导入该系统的流程。 今天,在调试导入过
我正在开发一个使用 C# 和 SQL Server 作为数据库的 .NET Web 应用程序,我是这两种技术的新手。当我尝试同时保存大量信息时遇到问题。 我的代码很简单: SqlConnectio
您好,我在我的应用程序的多个位置使用了 TransactionScope。喜欢: using (var scope = new TransactionScope()) { ToDo1(
我在服务器端代码 (WCF) 中使用 TransactionScope 类时遇到性能问题。 我的代码从客户端获取请求,创建 TransactionScope 并执行短操作(通常最多 100 毫秒)。
我正在处理一个几乎不存在事务的遗留项目,这会在出现错误时导致大量的部分提交。 SQL 调用混合了原始 ADO、Linq to Entities 和 EntityFramework,我想让它们在事务中运
我是一名优秀的程序员,十分优秀!