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
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...
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With