1

Issue:

In VS Code, I created a .NET Web API application using Entity Framework. To activate .NET commands, I always have to run the following command:

export PATH="$PATH:$HOME/.dotnet/tools"

After doing this, I can successfully run migrations. However, after migration, when I try to update the database using the following command:

dotnet ef database update

I get the following 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: TCP Provider, error: 26 - Error Locating Server/Instance Specified)

appsettings.json:

{
  "ConnectionStrings": {
    "DefaultConnection": "Data Source=Khashayar\\SQLEXPRESS;Initial Catalog=FinanceNews;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*"
}

Program.cs Injection:

builder.Services.AddDbContext<ApplicationDbContext>(options =>
{
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"));
});

Installed Packages:

  • Microsoft.AspNetCore.OpenApi 8.0.8
  • Microsoft.EntityFrameworkCore.Design 8.0.8
  • Microsoft.EntityFrameworkCore.SqlServer 8.0.8
  • Microsoft.EntityFrameworkCore.Tools 8.0.8

Additional Info:

  • I am using Ubuntu Terminal.
  • I am using .NET 8, SQL Server Management Studio (SSMS) 20, and SQL Server 2022 Configuration Manager.
  • In SSMS, I checked "Trust Server Certificate," and encryption is set to mandatory mode.
  • Services are running, and the data source name is correct (I verified this by running hostname in the terminal).

Troubleshooting Steps Taken:

  1. I enabled TCP/IP, but it didn’t work.

  2. I tried setting the data source to KHASHAYAR\\SQLEXPRESS, but it didn’t work.

  3. I also tried KHASHAYAR\\khash\\SQLEXPRESS, but it didn’t work either.

  4. I installed the necessary tools:

    sudo apt-get update
    sudo apt-get install mssql-tools unixodbc-dev
    echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
    source ~/.bashrc
    

    But it still didn’t work. I need Help to make update command work

Screenshots:

1
  • After you enabled the TCP/IP protocol in SQL Server Configuration manager, did you restart both the SQL Server Express instance and the SQL Browser service? Server protocol configuration changes do not take immediate effect, you have to restart the associated instance. Likewise the SQL Browser service, responsible for resolving instance names like SQLEXPRESS to the port(s) that instance is listening on, only loads the server protocol configuration when it gets started so it needs a restart after configuration changes. Commented Oct 3, 2024 at 23:41

1 Answer 1

1

As a solution I can suggest using update command with specifying connection string directly:

dotnet ef database update --connection '"Data Source=Khashayar\\SQLEXPRESS;Initial Catalog=FinanceNews;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"'

Where to look for potential issue:

  • if your project, that references EF, is not the main project with appsettings, you might need to indicate startup project in dotnet command:
    dotnet ef database update --startup-project {project with appsettings}
    
  • make sure you can connect to your database server with some client tool (Sql Management Studio, or other, or from command line, try pings)
Sign up to request clarification or add additional context in comments.

5 Comments

I followed your instructions and still I have same problem, even direct update did not work @AlwaysLearning
I followed your instructions and still I have same problem, even direct update did not work @Michal_Turczyn
So maybe your database is not avaialble ? Did you try pinging?
@Michal_Turczyn Yes I tried ping packages.microsoft.com and in response I get : PING s-part-0036.t-0009.t-msedge.net (13.107.246.64) 56(84) bytes of data. 64 bytes from 13.107.246.64 (13.107.246.64): icmp_seq=20 ttl=120 time=11.4 ms 64 bytes from 13.107.246.64 (13.107.246.64): icmp_seq=35 ttl=120 time=13.1 ms
@Michal_Turczyn on Ubunto I do not know if there is something I have to do to locate SQL , but I run this command : sudo apt-get update and I get E: Unable to locate package mssql-server. I followed an instructions to remove and reinstall , everytime I get E: Unable to locate package mssql-server. I do not know what to do to make ubunto connected to sql

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.