底层提供商在开放时失败

时间:2014-04-25 08:58:13

标签: sql asp.net-mvc linq nunit sqlexception

我有一个测试,我从NUnit运行,但它一直在失败

    [Test]
    public void CheckTeamsExist()
    {
        UsersContext context = new UsersContext();

        var count = context.Events.Count();

        Assert.AreSame(true, count > 10);

    }

我得到的确切错误消息是

QUBBasketballMVC.Tests.Controllers.TeamContextTest.CheckTeamsExist: System.Data.EntityException : The underlying provider failed on Open. ----> System.Data.SqlClient.SqlException : An attempt to attach an auto-named database for file C:\Users\connor\Desktop\dissertation_project (ASSEMBLA)\branches\interim_demo\Qub_Website\QUBBasketballMVC.Tests\bin\Debug\qub_db.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

    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.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
           at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
           at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
           at System.Data.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__3[TResult](IEnumerable`1 sequence)
           at System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot)
           at System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[S](Expression expression)
           at System.Linq.Queryable.Count[TSource](IQueryable`1 source)
           at QUBBasketballMVC.Tests.Controllers.AccountControllerTest.ExampleForMockingAccountMembershipServiceNEW() in c:\Users\connor\Desktop\dissertation_project (ASSEMBLA)\branches\interim_demo\Qub_Website\QUBBasketballMVC.Tests\Controllers\AccountControllerTest.cs:line 109
      InnerException: System.Data.SqlClient.SqlException
           HResult=-2146232060
           Message=An attempt to attach an auto-named database for file C:\Users\connor\Desktop\dissertation_project (ASSEMBLA)\branches\interim_demo\Qub_Website\QUBBasketballMVC.Tests\bin\Debug\qub_db.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
           Source=.Net SqlClient Data Provider
           ErrorCode=-2146232060
           Class=14
           LineNumber=65536
           Number=15350
           Procedure=""
           Server=(LocalDB)\v11.0
           State=1
           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.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
                at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
                at System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK)
                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)
                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, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
                at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnectionOptions userOptions)
                at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnectionOptions userOptions)
                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& connection)
                at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
                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: 

看不出我哪里错了!这段代码可以在我的主项目中正常工作

<connectionStrings>
<add name="qub_dbEntities1" connectionString="metadata=res://*/Models.entityDataModel.csdl|res://*/Models.entityDataModel.ssdl|res://*/Models.entityDataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\qub_db.mdf;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />

</connectionStrings>

我在两个配置文件中都有这个连接字符串。

我的单元测试项目中DataDirectory /实体框架文件的位置是不一样的问题?

1 个答案:

答案 0 :(得分:2)

要添加Lilshieste的评论, 将DataDirectory g测试项目中的app.confi占位符替换为App Data文件夹的硬编码路径。

如果将数据库文件(mdf和ldf)复制到测试项目的bin / debug文件夹,它也可以工作。