update-database: "A network-related or instance-specific error occurred while establishing a connection to SQL Server"
Solution 1
I finally found out. I have a website project and my API in my solution. The problem was that the website was set as startup project. So that entity framework searched the connection string in the website's web.config
and not in the api's web.config
. Changing the startup project to my API solved the problem.
If you want to have the website project as startup project. Just copy the connection string into the websides web.config
.
Nevertheless thanks for everyones help.
Solution 2
Also for migrations you can use OnConfiguring EntityFramework will check it by default. Add this code to your DbContext.
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"connection_string_here");
}
Related videos on Youtube
Comments
-
Anaa over 1 year
I have a simple C# project. This is my connection string in my
web.config
for the database:<connectionStrings> <add name="DefaultConnection" connectionString="Data Source=172.17.0.47;Initial Catalog=DevSystemListe;User ID=web_access;Password=123456" providerName="System.Data.SqlClient" /> </connectionStrings>
I already made sure that this connection is working. I can connect to my database from the Visual Studio with this connection and I can also see the tables and data.
When I want to update my database with
update-database
, this error occurs: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: 26 - Error Locating Server/Instance Specified)
I uninstalled Entity Framework and then installed it again. Nothing changed.
-
Anaa almost 9 yearsYes, my user does have the permissions. Thanks anyway.
-
jb007 over 7 yearsChange Startup Project worked for me as well. Thanks.
-
Casey Crookston almost 7 yearsBam! Thank you for answering your own question. I REALLY appreciate that! You just solved my issue after I'd been looking at other threads and trying various solutions. Thank you!
-
Sasha Bond over 6 yearsThis response is irrelevant to the question. Changing startup project does solve the problem.
-
Marc.2377 over 5 yearsThis sucks, I had already set the "Default Project" correctly, wasn't expecting the migration to try and connect based on the Startup project's settings. Besides, I actually can't change the startup project, unless temporarily. Just spent an hour and half troubleshooting this.
-
Gert Arnold about 3 yearsYou don't want a hard-coded connection string there. And if you get it from the configuration then you run into the same issue.