27

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Anaa
  • 1,191
  • 1
  • 9
  • 21
  • The SQL server is binding to the default port (1433)? – fishmong3r Jul 07 '15 at 09:23
  • @fishmong3r Yes it is. – Anaa Jul 07 '15 at 09:34
  • What is the name of connection string which you passed to the `DbContext` class constructor ? – Mohsen Esmailpour Jul 07 '15 at 09:35
  • @mo.esmp "DefaultConnection", the same as the name of the connection string. – Anaa Jul 07 '15 at 09:40
  • 1
    Related posts - [Why am I getting “Cannot Connect to Server - A network-related or instance-specific error”?](https://stackoverflow.com/q/18060667/465053) & [How do I fix the error 'Named Pipes Provider, error 40 - Could not open a connection to' SQL Server'?](https://stackoverflow.com/q/9945409/465053) – RBT Aug 30 '19 at 08:39

5 Answers5

70

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.

Anaa
  • 1,191
  • 1
  • 9
  • 21
  • 3
    Change Startup Project worked for me as well. Thanks. – Judy007 Oct 09 '16 at 22:36
  • 4
    Bam! 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! – Casey Crookston May 17 '17 at 16:57
  • This 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. – Marc.2377 Jul 31 '18 at 21:25
1

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");
}
  • You don't want a hard-coded connection string there. And if you get it from the configuration then you run into the same issue. – Gert Arnold Feb 03 '21 at 14:52
0

You say you can use this connection string to see data & tables... can you also use this connection to insert new records ?

For each user, SQL Server lets you define if they have read access and/or write access. Perhaps your user is just missing the db_datawriter permission...?

enter image description here

Mike Gledhill
  • 27,846
  • 7
  • 149
  • 159
0

If the application is being run under full IIS, then there is some configuration for IIS needed to ensure that the localDB instance can be used in the application. See https://blogs.msdn.microsoft.com/sqlexpress/2011/12/08/using-localdb-with-full-iis-part-1-user-profile/

P Walker
  • 532
  • 5
  • 15
0

This is a late response, but I determined that the cause of this error for me was that I had tried to eliminate an extra web.config connection string without making a necessary change to my DbContext class.

I had one connection string for my DefaultConnection and another generated by EF Migrations.

<connectionStrings>
    <!-- PRODUCTION-->
    <add name="DefaultConnection" connectionString="…" providerName="System.Data.SqlClient"/>
    <add name="Clients.Link.Models.ClientsLinkDB" connectionString="…" providerName="System.Data.SqlClient"/>
</connectionStrings>

I decided to eliminate the second connection string thinking that EF would fall back or was already using my DefaultConnection.

<connectionStrings>
    <!-- PRODUCTION-->
    <add name="DefaultConnection" connectionString="…" providerName="System.Data.SqlClient"/>
</connectionStrings>

When I attempted to run update-database, I got the error message reported above:

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)

The fix turned out to be extremely simple. I simply needed to instruct EF to use my DefaultConnection connection string instead of the one generated by migrations.

To do that, you just need to create or edit the constructor of your DbContext and pass the name of the DefaultConnection as a string to the base class constructor:

public class ClientsLinkDB : DbContext
{
    public ClientsLinkDB() : base("DefaultConnection")
    {
        
    }

    // The rest of your implementation follows
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
jlavallet
  • 1,267
  • 1
  • 12
  • 33