martincostello/sqllocaldb

Cannot open database requested by the login. The login failed.

rosdi opened this issue · 6 comments

rosdi commented

Hi,

I am trying to adapt the sample BlogSample.Tests project into my own project, I copied TestSetup.cs and all but keep getting this error.... any ideas what caused it?

I am certain it isn't SQL Server Configuration issue because the BlogSample.Tests project works fine on my computer... all tests pass but when I tried to copy those into my project I will get this error...

Test method Tests.UnitSpecs.Web.Controllers.FpxControllerSpecs.get_transaction_status_should_return_one_result threw exception: 
System.Data.Entity.Core.EntityException: The underlying provider failed on Open. ---> System.Data.SqlClient.SqlException: Cannot open database "Blog_2016-09-23-10-09-54_1d5233f6-bb51-46bb-82b4-37b6b00fddfb" requested by the login. The login failed.
Login failed for user 'PC\rosdi'.
    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.Entity.Infrastructure.Interception.DbConnectionDispatcher.<Open>b__36(DbConnection t, DbConnectionInterceptionContext c)
   at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
   at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext)
   at System.Data.Entity.Core.EntityClient.EntityConnection.<Open>b__2()
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.<Execute>b__0()
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation)
   at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
 --- End of inner exception stack trace ---
    at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
   at System.Data.Entity.Core.Objects.ObjectContext.EnsureConnection(Boolean shouldMonitorTransactions)
   at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass7.<GetResults>b__5()
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
   at System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()
   at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
   at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
   at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.<GetElementFunction>b__3[TResult](IEnumerable`1 sequence)
   at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 query, Expression queryRoot)
   at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression expression)
   at System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expression expression)
   at System.Linq.Queryable.Single[TSource](IQueryable`1 source, Expression`1 predicate)
   at Business.SettingsService`1.Find(String id) in C:\Projects\FPX\Business\SettingsService.cs:line 36
   at FPX.Controllers.BaseController.GetSecureSecretForSystemId(String systemId) in C:\Projects\FPX\Web\Controllers\BaseController.cs:line 65
   at FPX.Controllers.BaseController.GenerateQueryHashBySystemId(Int32 transactionId, String systemId, String buyerBankId, Double totalAmount, String transactionDesc, String returnUrl, String buyerEmail, String extraInfo1, String extraInfo2) in C:\Projects\FPX\Web\Controllers\BaseController.cs:line 30
   at Tests.UnitSpecs.Web.Controllers.FpxControllerSpecs.<get_transaction_status_should_return_one_result>d__2.MoveNext() in C:\Projects\FPX\Tests\UnitSpecs\Web\Controllers\FpxControllerSpecs.cs:line 40
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()

Here is the sql connection string generated by the code (which is TestSetup.cs that I copied from BlogSample.Tests:

Data Source=np:\\.\pipe\LOCALDB#0FCA1F41\tsql\query;Initial Catalog=Blog_2016-09-23-10-09-54_1d5233f6-bb51-46bb-82b4-37b6b00fddfb
rosdi commented

Here is a minimal project (TestApp.zip) that exhibits this issue.

TestApp.zip

Are you inheriting a connection string that the tests are using for templating? You might need Integrated Security=True; adding if it isn't there so that the connection string is set up appropriately.

If it's not that, I'll take a look at your sample later today and see if I can work it out for you.

rosdi commented

Nop... I just passed the connection string to my repository service... and I also tried hard coding Integrated Security=True; into the generated connection string but nothing works...

Btw, I am using this nuget package to manage my entities: https://www.nuget.org/packages/Mehdime.Entity/)

I also tried with this https://github.com/joshclark/TemporaryDb and it is working fine... if you are busy that's ok... I will use TemporaryDb for now... if somehow I found out the cause I will post it here, hopefully it will be useful to somebody else.

I'm getting the same behaviour as you where it can't open a connection to your database as the login failed. Where exactly in your code is your new empty database supposed to be being created? If I connect to the SQL LocalDB instance created by the tests the only databases present are these ones:

image

That would explain the login error, as you can't login to a database that doesn't exist.

In the blog sample, the database used for the tests is created automatically by EF. Is Mehdime.Entity supposed to create the database for you?

rosdi commented

That's the reason?... I guess I was confused by the error message... even the code TemporarySqlLocalDbInstance.Create seems to imply that a database has been created? no?...

I didn't set up the tables and such because I was actually expecting to see error message like dbo.<TableName> not exist or something similar... (the TemporaryDb nuget I mentioned earlier did, in fact, spit this error on the first try...)

I wasted half a day hunting this... thinking it was my sql database configuration issue lol...

Yeah, unfortunately not. This library just manages SQL LocalDB instances, which are effectively mini SQL servers. What goes in the server (databases, users etc.) is all left to the consumer.

You raise a good point though that the example's use of EF does create some "black magic" ambiguity.

The text of the error message is SQL server's doing though. I imagine it's worded in such a way to stop information disclosure via probing for whether or not a database with a specific name exists in a server.