You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu环境下.NET 8 EF执行dotnet ef database update连接SQL Server失败求助

问题描述

在VS Code中使用Entity Framework创建.NET Web API应用,每次需要执行以下命令激活.NET命令:

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

执行后可成功运行迁移,但执行更新数据库命令时出现错误:

dotnet ef database update

错误信息:

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依赖注入代码

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

已安装的NuGet包

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

补充信息

  • 操作系统:Ubuntu,通过终端操作
  • 环境:.NET 8、SQL Server 2022、SSMS 20
  • SSMS设置:已勾选“信任服务器证书”,加密模式为强制
  • SQL Server服务正在运行,数据源名称已通过hostname命令验证正确

已尝试的排查步骤

  1. 启用TCP/IP,无效
  2. 修改数据源为KHASHAYAR\\SQLEXPRESS,无效
  3. 修改数据源为KHASHAYAR\\khash\\SQLEXPRESS,无效
  4. 安装工具:
    sudo apt-get update
    sudo apt-get install mssql-tools unixodbc-dev
    echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
    source ~/.bashrc
    
    问题仍未解决

解决方案

1. 修正连接字符串加密配置

SSMS已设置强制加密,但连接字符串中Encrypt=False会导致连接失败,修改为:

"DefaultConnection": "Data Source=Khashayar\\SQLEXPRESS;Initial Catalog=FinanceNews;Integrated Security=True;Connect Timeout=30;Encrypt=True;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"

强制加密模式下必须开启Encrypt=True,TrustServerCertificate=True可绕过Ubuntu上的证书信任问题。

2. 改用IP+端口连接(避免实例名称解析问题)

  • 打开SQL Server配置管理器,找到SQL Server网络配置 -> SQLEXPRESS的协议 -> TCP/IP
  • 右键属性,在「IP地址」标签中设置IPAll的TCP端口为固定值(如1433),重启SQL Server服务
  • 修改连接字符串为IP+端口形式:
    "DefaultConnection": "Data Source=Khashayar,1433;Initial Catalog=FinanceNews;Integrated Security=True;Connect Timeout=30;Encrypt=True;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
    

3. 验证网络连通性

在Ubuntu终端执行以下命令,确认能连接到SQL Server端口:

nc -zv Khashayar 1433

如果不通,检查Windows防火墙是否允许1433端口入站,或SQL Server是否绑定了允许外部访问的IP(而非仅localhost)。

4. 改用SQL Server认证(避免Kerberos配置问题)

Ubuntu上使用Integrated Security=True需配置Kerberos,若嫌麻烦可改用SQL Server账号认证:

  • 在SSMS中创建SQL Server登录账号并设置密码
  • 修改连接字符串:
    "DefaultConnection": "Data Source=Khashayar,1433;Initial Catalog=FinanceNews;User ID=你的账号;Password=你的密码;Connect Timeout=30;Encrypt=True;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
    

5. 永久配置.NET Tools路径

将激活命令加入~/.bashrc,避免每次手动执行:

echo 'export PATH="$PATH:$HOME/.dotnet/tools"' >> ~/.bashrc
source ~/.bashrc

内容的提问来源于stack exchange,提问作者Khashayar Rohani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 18:53:20