While running MVC 5 application I tried using SQL server 2014 LocalDB, but when I clicked the registration link and submitted the details I got the below error.
So what is Localdb?
Localdb is a light weight version of SQL server express. When we install Visual studio 2012 or above LocalDb is installed by default. When we create MVC 5 Internet applications, it will use entity framework to manage Identity (User,Roles). Entity Framework by default use Localdb. This database is created in App_Data folder of a web project. LocalDB should not be used when we are moving the application to the production, as it will be present in web application App_Data folder. All the databases should be placed in database server. In such a case we can easily migrate LocalDb to SQL Server by changing the connection string. When we run an application using code first approach, models are created first and from models we create the database, if the database is not there Entity Framework will create it for us.
Now coming back to the issue. After models were created and I ran the application and clicked Register link and submitted the details I got the below error.
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible.
After searching on Google I found from the Microsoft website that SQL server 2014 localdb versions will not be like v11.0; now they have changed the localdb instance as MSSQLLocalDB. So I was unable to connect to (LocalDB)\v11.0.
Below are the two connection string versions that are to be used for connecting to SQL server localdb.
For SQL Server 2012,
- <connectionstrings>
- <add connectionstring="Data Source=(LocalDB)\v11.0;
- AttachDbFilename=|DataDirectory|\aspnet-AspnetIdentitySample-20130627083537_2;
- Integrated Security=True;Integrated Security=True" name="DefaultConnection" providername="System.Data.SQLClient">
- </add>
- </connectionstrings>
For SQL Server 2014.
- <connectionstrings>
- <add connectionstring="Data Source=(LocalDB)\MSSQLLocalDB;
- AttachDbFilename=|DataDirectory|\aspnet-AspnetIdentitySample-20130627083537_2;
- Integrated Security=True;Integrated Security=True"
- name="DefaultConnection" providername="System.Data.SQLClient">
- </add>
- </connectionstrings>