簡體   English   中英

用戶代碼未處理System.Data.EntityException

[英]System.Data.EntityException was unhandled by user code

我對C#,ASP,.NET,Visual Studio或Azure的經驗很少。 我有一個Web應用程序,當將其部署到Azure時運行良好(我仍然不了解此過程),但是當我在Visual Studio 2012中運行Debug-> Start Debugging時,出現此錯誤:

System.Data.EntityException was unhandled by user code
  HResult=-2146233087
  Message=The underlying provider failed on Open.
  Source=System.Data.Entity
  StackTrace:
       at System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure)
       at System.Data.EntityClient.EntityConnection.Open()
       at System.Data.Objects.ObjectContext.EnsureConnection()
       at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
       at System.Data.Entity.Internal.InternalContext.SaveChanges()
       at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
       at System.Data.Entity.DbContext.SaveChanges()
       at Awesomeness.Core.BaseController.AddLog(String key, String message) in c:\Users\zack\Code\Awesomeness Reminders\AwesomenessWeb\AwesomenessWeb\Core\BaseController.cs:line 76
       at Awesomeness.Core.BaseController.OnActionExecuting(ActionExecutingContext filterContext) in c:\Users\zack\Code\Awesomeness Reminders\AwesomenessWeb\AwesomenessWeb\Core\BaseController.cs:line 26
       at System.Web.Mvc.Controller.System.Web.Mvc.IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
       at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14()
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
  InnerException: System.Data.SqlClient.SqlException
       HResult=-2146232060
       Message=A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
       Source=.Net SqlClient Data Provider
       ErrorCode=-2146232060
       Class=20
       LineNumber=0
       Number=2
       Server=""
       State=0
       StackTrace:
            at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
            at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
            at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover)
            at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover)
            at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)
            at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
            at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData)
            at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
            at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
            at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
            at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
            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.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure)
       InnerException: System.ComponentModel.Win32Exception
            HResult=-2147467259
            Message=The system cannot find the file specified
            ErrorCode=-2147467259
            NativeErrorCode=2
            InnerException: 

我將感謝有關如何整理的提示,以及有關基本背景知識的建議。

謝謝。

編輯:

我在Web.config中的連接字符串是:

    <!-- local -->
    <add name="Entities" connectionString="metadata=res://*/Models.Awesomeness.csdl|res://*/Models.Awesomeness.ssdl|res://*/Models.Awesomeness.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=Awesomeness;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

對我來說,在Web配置文件中,從ConnectionString刪除了

 integrated security=True;

輕而易舉!

在調試中檢查您的連接字符串。

單擊工具>連接到數據庫。 嘗試連接到sql server數據庫。 如果不能,則表示它不是連接字符串。 我得到這個錯誤

 "A network-related or instance-specific error occurred while establishing a connection to SQL Server."

因此,您可以搜索該錯誤,在線上有大量幫助。

如果能夠在Visual Studio中連接到數據庫,則單擊數據庫,然后在屬性窗口中將看到連接字符串。 您可以將連接字符串復制並粘貼到您的應用程序中,然后您會知道它是正確的。

暫無
暫無

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

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