C# 提交/回滚从业务层到存储库层的多个数据库事务调用
我有表示层/业务层/存储库层。所有数据库调用,如select/update/insert/delete,都是在存储库层完成的。业务层检查规则,并根据存储库层进行调用以与数据库交互。我遇到的问题是,每当调用中出现问题时,在业务层回滚事务 我确实参考了一些文章,如和,但我使用transactionscope获得System.Transactions.TransactionException错误 编辑:- 这就是我从业务层调用存储库层的方式C# 提交/回滚从业务层到存储库层的多个数据库事务调用,c#,sql,asp.net-mvc-4,transactions,entity-framework-6,C#,Sql,Asp.net Mvc 4,Transactions,Entity Framework 6,我有表示层/业务层/存储库层。所有数据库调用,如select/update/insert/delete,都是在存储库层完成的。业务层检查规则,并根据存储库层进行调用以与数据库交互。我遇到的问题是,每当调用中出现问题时,在业务层回滚事务 我确实参考了一些文章,如和,但我使用transactionscope获得System.Transactions.TransactionException错误 编辑:- 这就是我从业务层调用存储库层的方式 public bool CreatePerson(Model
public bool CreatePerson(Model AddPersonToSchool)
{
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, TimeSpan.FromSeconds(120)))
{
try
{
//Check if the person is already created, if not assign him the next Roll call and details
CreatePersonDetails(Person); //Repository call where SaveChanges() are called.
//Check the preReq for the subjects
CreatePersonSubjects(Person); //Repository call where SaveChanges() are called.
//Check for the Availability of the seats in classes
CreateTimeTableForClasses(Person); //Repository call where SaveChanges() are called.
LogDetails(Person);
scope.Complete();
return true;
}
catch (Exception ex)
{
scope.Dispose();
RecordErrorException(ex);
return false;
}
}
}
这是我收到的错误消息
Exception type: System.Transactions.TransactionException
Message : The operation is not valid for the state of the transaction.
Stacktrace:
at System.Transactions.TransactionState.EnlistPromotableSinglePhase(InternalTransaction tx, IPromotableSinglePhaseNotification promotableSinglePhaseNotification, Transaction atomicTransaction)
at System.Transactions.Transaction.EnlistPromotableSinglePhase(IPromotableSinglePhaseNotification promotableSinglePhaseNotification)
at System.Data.SqlClient.SqlInternalConnection.EnlistNonNull(Transaction tx)
at System.Data.SqlClient.SqlInternalConnection.Enlist(Transaction tx)
at System.Data.SqlClient.SqlInternalConnectionTds.Activate(Transaction transaction)
at System.Data.ProviderBase.DbConnectionInternal.ActivateConnection(Transaction transaction)
at System.Data.ProviderBase.DbConnectionPool.PrepareConnection(DbConnection owningObject, DbConnectionInternal obj, Transaction transaction)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.<Open>b__38(DbConnection t, DbConnectionInterceptionContext c)
at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext)
at System.Data.Entity.Core.EntityClient.EntityConnection.<Open>b__2()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.<Execute>b__0()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation)
at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
修复可以简单到将类型值更改为其他值,但我们需要知道您得到的确切错误消息以及不同层如何相互调用。mcve真的可以帮助任何人检查你的问题。如果你需要处理常规存储库的事务,那么从根本上说有些事情是错误的。应该可以通过一个SaveChanges调用提交几乎所有的业务事务,而不管它埋在存储库/UoW层下有多深。也就是说,如果您对代码有问题,请显示代码。这个问题无法回答。看看这个:。您的代码没有透露任何相关细节,我们无法诊断问题。