簡體   English   中英

底層提供程序在 Open EF6 iis 上失敗

[英]The underlying provider failed on Open EF6 iis

在我將程序升級到.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, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   在 System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
   在 System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, 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都已經dispose了,不明白哪里出錯了
這是我的連接字符串

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;

        }

我們有一個類似的問題,我們通過在數據庫服務器中添加多個活動結果集來解決它。

"Multiple Active Result Sets (MARS) is a feature that works with SQL Server to allow the execution of multiple batches on a single connection. When MARS is enabled for use with SQL Server, each command object used adds a session to the connection."

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM