LocalDB instance specified does not exist
Asked Answered
Q

3

6

I am trying to build an ASP.NET MVC 5 Web Application in SQL Server 2015 Express using EntityFramework 6 but when I debug the application and go to a page where the database is needed I get the following error.

System.Data.SqlClient.SqlException was unhandled by user code ErrorCode=-2146232060 HResult=-2146232060 LineNumber=0 Message=Error relacionado con la red o específico de la instancia mientras se >establecía una conexión con el servidor SQL Server. No se encontró el servidor >o éste no estaba accesible. Compruebe que el nombre de la instancia es correcto >y que SQL Server está configurado para admitir conexiones remotas. (provider: >SQL Network Interfaces, error: 50 - Se produjo un error de Local Database >Runtime. La instancia de LocalDB especificada no existe.)

This error mentions that there is a remote conection problem but this is not posible because i am in local. Then I get the following error: The localDB instance Local Database Runtime error occurred. LocalDB instance specified does not exist.

StackTrace:

    en System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling)
       en System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
       en System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
       en System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
       en System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
       en System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
       en System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
       en System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
       en System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
       en System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
       en System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
       en System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
       en System.Data.SqlClient.SqlConnection.Open()
       en System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.<Open>b__36(DbConnection t, DbConnectionInterceptionContext c)
       en System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
       en System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext)
       en System.Data.Entity.SqlServer.SqlProviderServices.<>c__DisplayClass33.<UsingConnection>b__32()
       en System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.<Execute>b__0()
       en System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
       en System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation)
       en System.Data.Entity.SqlServer.SqlProviderServices.UsingConnection(DbConnection sqlConnection, Action`1 act)
       en System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection(DbConnection sqlConnection, Action`1 act)
       en System.Data.Entity.SqlServer.SqlProviderServices.CreateDatabaseFromScript(Nullable`1 commandTimeout, DbConnection sqlConnection, String createDatabaseScript)
       en System.Data.Entity.SqlServer.SqlProviderServices.DbCreateDatabase(DbConnection connection, Nullable`1 commandTimeout, StoreItemCollection storeItemCollection)
       en System.Data.Entity.Core.Common.DbProviderServices.CreateDatabase(DbConnection connection, Nullable`1 commandTimeout, StoreItemCollection storeItemCollection)
       en System.Data.Entity.Core.Objects.ObjectContext.CreateDatabase()
       en System.Data.Entity.Migrations.Utilities.DatabaseCreator.Create(DbConnection connection)
       en System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
       en System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
       en System.Data.Entity.Internal.DatabaseCreator.CreateDatabase(InternalContext internalContext, Func`3 createMigrator, ObjectContext objectContext)
       en System.Data.Entity.Internal.InternalContext.CreateDatabase(ObjectContext objectContext, DatabaseExistenceState existenceState)
       en System.Data.Entity.Database.Create(DatabaseExistenceState existenceState)
       en System.Data.Entity.CreateDatabaseIfNotExists`1.InitializeDatabase(TContext context)
       en System.Data.Entity.Internal.InternalContext.<>c__DisplayClassf`1.<CreateInitializationAction>b__e()
       en System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
       en System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
       en System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c)
       en System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input)
       en System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action)
       en System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
       en System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
       en System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
       en System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator()
       en System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator()
       en System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       en System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       en WebApplication10.Controllers.AlbumsController.Index() en C:\Users\Develop2\Documents\Visual Studio 2015\Projects\WebApplication10\WebApplication10\Controllers\AlbumsController.cs:línea 20
       en lambda_method(Closure , ControllerBase , Object[] )
       en System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
       en System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
       en System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
       en System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
       en System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
       en System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
       en System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
       en System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
       en System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()

EDIT
My connection Strings looks like:

 <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-WebApplication10-20151208105118.mdf;Initial Catalog=aspnet-WebApplication10-20151208105118;Integrated Security=True" providerName="System.Data.SqlClient" />
    <add name="WebApplication10Context" connectionString="Data Source=(localdb)\MSSQLLocalDB; Initial Catalog=WebApplication10Context-20151208105514; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|WebApplication10Context-20151208105514.mdf" providerName="System.Data.SqlClient" />
  </connectionStrings>

EDIT 2

<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
    <parameters>
      <parameter value="v11.0" />  
    </parameters>
     </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>

What should be the problem?

SOLUTION

As Cigano Morrison suggested, the problem were that the instance of localDB was missing so I had to run in the command line the following command sqllocaldb c MSSQLLocalDB 11.0

Quadrennium answered 8/12, 2015 at 15:3 Comment(3)
Can you please edit your question with the connection string used to perform the connection?Oculo
Yeah ..it seems to be a connection string problem.Baumann
@CiganoMorrisonMendez I edit my post with the connection stringQuadrennium
O
2

First of all, check if your project has the files aspnet-WebApplication10-20151208105118.mdf and WebApplication10Context-20151208105514.mdf in App_Data folder (usually, they are not part of the solution.

Second, try to replace (LocalDb)\MSSQLLocalDB by (LocalDb)\v11.0.

Third, a backslash is missing in the second connection string:

<add name="WebApplication10Context" connectionString="Data Source=(localdb)\MSSQLLocalDB; Initial Catalog=WebApplication10Context-20151208105514; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|\WebApplication10Context-20151208105514.mdf" providerName="System.Data.SqlClient" />

Fourth, check if you have an installation of SQL Server with Advanced Features, which includes LocalDB.


To create a new Database file, right-click on App_Data folder > Add > New Item...

New SQL

Oculo answered 8/12, 2015 at 15:38 Comment(4)
I checked the App_Data folder but is empty. How does I generate these .mdf files?Quadrennium
When I try to create any aspnet-WebApplication10-20151208105118.mdf or WebApplication10Context-20151208105514.mdf database files I get the same error Local Database Runtime error occurred. LocalDB instance specified does not existQuadrennium
@Quadrennium Please check this answer.Oculo
Your solution worked! I only need to run in the cmd the command sqllocaldb c MSSQLLocalDB 11.0 Thanks!Quadrennium
C
1

Just create the instance using this cmd command

sqllocaldb create "MSSQLLocalDB"

or by default sqllocaldb comes with an instance which is v11 or v12 folowing your sqlserver version, if you would to use it just replace (localdb)\MSSQLLocalDB by (localdb)\v11

Chore answered 16/9, 2019 at 13:18 Comment(0)
M
0

If you are using LocalDB you can actually use a command line tool by Microsoft to view your instances, their end points and start and stop the instances as you need. You can also use SQL Server management studio to connect to your LocalDB instances outside of Visual Studio to work in an environment that is more suitable for SQL operations. I have detailed this all in my blog post here http://www.nikgupta.net/2015/12/connect-localdb-from-management-studio/

Malapert answered 11/12, 2015 at 6:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.