将程序升级到.net 4.5,将EntityFramework升级到最新版本并使用Autofac对应用程序进行一些调整之后,我遇到了以下问题:
该程序运行了几天后,突然会开始频繁报告:
System.Data.Entity.Core.EntityException: The underlying provider failed on Open. ---> System.InvalidCastException: Cannot cast an object of type "System.Data.SqlClient.SqlTransaction" to type "System.Transactions.Safeiunknown".。 在 System.Transactions.Transaction.JitSafegetcontextTransaction(ContextData contextData) 在 System.Transactions.Transaction.FastGetTransaction(TransactionScope currentScope,ContextData contextData,Transaction& contextTransaction) 在 System.Transactions.Transaction.get_Current() 在 System.Data.ProviderBase.DbConnectionPool.GetFromTransactedPool(Transaction& transaction) 在 System.Data.ProviderBase.DbConnectionPool.Trygetconnection(DbConnection owningObject,UInt32 waitForMultipleObjectsTimeout,Boolean allowCreate,Boolean onlyOneCheckConnection,DbConnectionOptions userOptions,DbConnectionInternal& connection) 在 System.Data.ProviderBase.DbConnectionPool.Trygetconnection(DbConnection owningObject,TaskCompletionSource`1 retry,DbConnectionInternal& connection) 在 System.Data.ProviderBase.DbConnectionFactory.Trygetconnection(DbConnection owningConnection,DbConnectionInternal oldConnection,DbConnectionInternal& connection) 在 System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection,DbConnectionFactory connectionFactory,DbConnectionOptions userOptions) 在 System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry) 在 System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) 在 System.Data.SqlClient.SqlConnection.Open() 在 System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext](TTarget target,action`2 operation,TInterceptionContext interceptionContext,action`3 executing,action`3 executed) 在 System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection,DbInterceptionContext interceptionContext) 在 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.c__DisplayClass2_0.b__0() 在 System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation) 在 System.Data.Entity.Core.EntityClient.EntityConnection.Open()
我认为这应该是由于将程序更改为依赖注入而引起的,但是我在Dbcontext创建和Dispose中创建了一个计数器,确认所有Dbcontext已经被处决,我不知道出了什么问题
这是我的连接字符串
Data Source=***; Initial Catalog=cloud; MultipleactiveResultSets=true; Pooling=True; UID=sa;PWD=***;
在某些地方,我使用交易,但是我很少使用它们。
public TransResult UseTrans(action<DbTransaction<TContext>> action)
{
if (currentTransHasError)
{
return new TransResult() { IsSuccess = false,Message = currentTransErrorMessage };
}
var result = new TransResult() { IsSuccess = true };
bool isnested = true;
try
{
if (currentContextTrans == null)
{
isnested = false;
currentContextTrans = db.Database.BeginTransaction();
}
var trans = new DbTransaction<TContext>(this,isnested);
action?.Invoke(trans);
trans.Commit();
}
catch (Exception e)
{
result.Message = currentTransErrorMessage = e.Message;
result.IsSuccess = false;
currentTransHasError = true;
if (currentContextTrans != null)
{
currentContextTrans.Rollback();
}
// TODO: Handle failure
}
finally
{
if (!isnested)
{
if (currentContextTrans != null)
{
currentContextTrans.Dispose();
currentContextTrans = null;
}
currentTransErrorMessage = string.Empty;
currentTransHasError = false;
}
}
return result;
}