我有一个使用TransactionScope
的函数。当我使用两个dbContext
时,它会崩溃。错误消息是:
System.Data.EntityException: Error underlying provider Open. ---> System.Transactions.TransactionException: The operation is not valid for the state of the transaction. in System.Transactions.TransactionState.EnlistPromotableSinglePhase (InternalTransaction tx, IPromotableSinglePhaseNotification promotableSinglePhaseNotification, Transaction AtomicTransaction, Guid promoterType) in System.Transactions.Transaction.EnlistPromotableSinglePhase (IPromotableSinglePhaseNotification promotableSinglePhaseNotification, Guid promoterType) in System.Transactions.Transaction.EnlistPromotableSinglePhase (IPromotableSinglePhaseNotification promotableSinglePhaseNotification) in System .Data.SqlClient.SqlInternalConnection.EnlistNonNull (Transaction tx) in System.Data.SqlClient.SqlInternalConnection.Enlist (Transaction tx)
in System.Data.ProviderBase.DbConnectionInternal.ActivateConnection (Transaction transaction) in System.Data.ProviderBase.DbConnectionPool.PrepareConnection (DbConnection owningObject, DbConnectionInternal obj, Transaction transaction) in System.Data.ProviderBase.DbConnectionPool.TryGetConnection (DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout , allowCreate Boolean, Boolean onlyOneCheckConnection, DbConnectionOptions UserOptions, DbConnectionInternal & connection) in System.Data.ProviderBase.DbConnectionPool.TryGetConnection (DbConnection owningObject, TaskCompletionSource1 retry, DbConnectionOptions
UserOptions, DbConnectionInternal & connection) in
System.Data.ProviderBase.DbConnectionFactory.TryGetConnection (DbConnection
owningConnection, TaskCompletionSource1 retry, DbConnectionOptions UserOptions, DbConnectionInternal oldConnection, DbConnectionInternal & connection) in System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal (DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions UserOptions) in
System.Data.SqlClient.SqlConnection.TryOpenInner (TaskCompletionSource1 retry) in System.Data.SqlClient.SqlConnection.TryOpen (TaskCompletionSource`1 retry) in System.Data.SqlClient.SqlConnection.Open () in System.Data.EntityClient.EntityConnection. OpenStoreConnectionIf (Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String ExceptionCode, attemptedOperation String, Boolean & closeStoreConnectionOnFailure)
代码
public void example(int c, IEnumerable<int> pi, string comm)
{
using (var transactionScope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.RepeatableRead }))
{
var idsList = pi.ToList();
foreach (var playlist in Context.pl.Where(p => idsList.Contains(p.pid) && p.c== c&& p.stat== 1))
{
playlist.TimestampAuthorized = null;
var pl = "sss";
notif.generateNotifForUser((new Notification(1, 1, pl, 2, 2)));
}
Context.SaveChanges();
transactionScope.Complete();
}
}
问题发生在Context.SaveChanges()
;
如果我对该行notif.generateNotifForUser();
发表评论,则可行。在这个函数中,我也做context.saveChanges();
由于
答案 0 :(得分:1)
// code before unchanged
foreach (var playlist in Context.pl.Where(p => idsList.Contains(p.pid) && p.c== c&& p.stat== 1).ToList()) // added ToList
// code after unchanged
问题与您尝试使用开放式连接进行两件事有关。迭代pl
DbSet结果以及您在generateNotifForUser
中正在进行的操作,这是您可能从DbContext获取/执行其他操作的内容(基于您的更新,您正在进行SaveChanges调用)。要解决此问题,请在Lamda语句的末尾添加ToList
,以强制在迭代开始之前立即完整地显示查询结果。然后,连接将不再打开,允许您继续在代码的其他部分使用DbContext
。这也假设你没有在方法链中进一步犯下类似的错误。
答案 1 :(得分:0)
编辑:
通知方法似乎导致错误,因为它也在调用Context.SaveChanges
您只需要拨打Context.SaveChanges
一次
我会删除通知方法中的Context.SaveChanges
,只在事务范围结束时调用一次。