如何在SQL Compact 4.0和NHibernate中使用TransactionScope [英] How can I use TransactionScope with SQL Compact 4.0 and NHibernate
问题描述
我正在尝试将NHibernate与.NET的TransactionScope对象结合使用.到目前为止,我已经成功地使用了Oracle 11g和SQL Server 2008R2,没有任何问题.但是,SQL Compact似乎已面目全非.
I am trying to use NHibernate in combination with .NET's TransactionScope object. So far I have successfully used Oracle 11g and SQL Server 2008R2 with no issues. However, SQL Compact seems to fall on its face.
using (var scope = new TransactionScope(TranactionScopeOption.Required))
{
using (var session = _sessionFactory.OpenSession())
{
// The line below throws. I also tried passing in System.Data.IsolationLevel.ReadCommitted to no avail
using (var txn = session.BeginTransaction())
{
// Perform insert
txn.Commit();
}
}
scope.Complete();
}
这导致以下异常.我知道这是什么意思,但我不明白为什么为什么尝试创建嵌套事务.
This results in the following exception. I understand what this means, but I don't understand why it is attempting to create a nested transaction.
NHibernate.TransactionException: Begin failed with SQL exception ---> System.InvalidOperationException: SqlCeConnection does not support nested transactions.
at System.Data.SqlServerCe.SqlCeConnection.BeginTransaction(IsolationLevel isolationLevel)
at System.Data.SqlServerCe.SqlCeConnection.BeginDbTransaction(IsolationLevel isolationLevel)
at System.Data.Common.DbConnection.System.Data.IDbConnection.BeginTransaction(IsolationLevel isolationLevel)
at NHibernate.Transaction.AdoTransaction.Begin(IsolationLevel isolationLevel)
at NHibernate.Transaction.AdoTransaction.Begin(IsolationLevel isolationLevel)
推荐答案
I think the answer lies in this question: The connection object can not be enlisted in transaction scope
猜测中,TransactionScope需要升级为分布式或嵌套事务,CE都不支持."
"At a guess, TransactionScope needs to escalate to a distributed or nested transaction, neither of which is supported by CE."
这篇关于如何在SQL Compact 4.0和NHibernate中使用TransactionScope的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!