Reputation: 1
I just started learning EF (not core) and was creating my first code-first application. This is a C# console application where all I am doing is adding a migration and then simply creating the sample database (same name as my solution). As I try to run Update-Database
, I get this 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. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Cannot create an automatic instance. See the Windows Application event log for error details.)
Steps I searched and implemented and have still not been able to resolve this are:
My connection string is:
<add name="ConsoleDBContext"
connectionString="Data Source=(LocalDb)\SQLEXPRESS;Initial Catalog=CodeFirst_Demo;Integrated Security=SSPI;"
providerName="System.Data.SqlClient" />
Also tried removing "localDB" but no avail.
The steps followed in the C# code using Package Manager Console are:
I would be extremely grateful for any help in this regard.
Upvotes: 0
Views: 640
Reputation: 1
Thanks everyone! My issue got resolved by explicitly entering the startup project name("CodeFirst_Demo" in my case) along the "Update-Database" command . Like the below :
update-database -StartupProjectName CodeFirst_Demo
Upvotes: 0
Reputation: 754200
The server/instance name in your connection string is rather curious....
Either you have the "full" SQL Server Express installed on your machine - the one that starts up when Windows launches and runs as a service in the background - in that case, you should use .\SQLEXPRESS
or (local)\SQLEXPRESS
as your server/instance name:
<add name="ConsoleDBContext"
connectionString="Data Source=(local)\SQLEXPRESS;Initial Catalog=CodeFirst_Demo;Integrated Security=SSPI;"
providerName="System.Data.SqlClient" />
Or alternatively, when you have SQL Server Express LocalDB installed (which gets installed with Visual Studio), then you should use (localdb)\MSSQLLocalDB
as your server/instance name:
<add name="ConsoleDBContext"
connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=CodeFirst_Demo;Integrated Security=SSPI;"
providerName="System.Data.SqlClient" />
The combination you are using right now obviously isn't correct - try one of these two and see if it works.
Upvotes: 1
Reputation: 221
Your server is not accessible to your program. If you running database on the same machine then your connection string should look like:
Data Source=.;Initial Catalog=<DB-Name>;User Id=<username>; Password=<pwd>;
Data source is the name of the server, the "." means localhost or 127.0.0.1 Initial Catalog is your database name, some times you may use master. and you don't need to configure TCP/IP on the same machine, unless you're hosting the database on other machine, then the Data Source should be db-machinname\db-instance-name, again if instance name is the default name then you just specify the machine name.
Upvotes: 0