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

Ubuntu系统下ASP.NET连接本地SQL Server执行EF更新失败求助

Ubuntu下ASP.NET连接SQL Server执行dotnet ef database update失败解决方案

核心问题定位

你的连接字符串存在两个关键问题:

  1. mssqllocaldb实例不适用于Linux环境:mssqllocaldb是Windows平台SQL Server Express专属的本地DB实例,Linux上的SQL Server默认是默认实例(无命名实例),不存在这个实例名。
  2. JSON中转义字符错误:连接字符串里的\在JSON配置文件中需要转义为\\,否则会被解析为无效转义字符,导致实例名识别错误。

排查与修复步骤

  • 修正连接字符串
    将原连接字符串中的Server=localhost\mssqllocaldb替换为以下任意一种:

    "ConnectionStrings": {
      "DefaultConnection": "Server=localhost,1433;Database=temaintdb;User Id=sa;Password=******;"
    }
    

    或(默认实例可省略端口):

    "ConnectionStrings": {
      "DefaultConnection": "Server=localhost;Database=temaintdb;User Id=sa;Password=******;"
    }
    
  • 验证数据库连接可用性
    使用sqlcmd工具直接测试连接,确认数据库本身可访问:

    sqlcmd -S localhost,1433 -U sa -P 你的密码
    

    若能成功进入SQL命令行,说明数据库服务正常,问题仅出在应用连接配置。

  • 确认EF Core配置正确性
    检查Program.cs或Startup.cs中是否正确绑定了连接字符串:

    builder.Services.AddDbContext<YourDbContext>(options =>
        options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
    
  • 重新执行EF命令
    修正配置后,重新运行:

    dotnet ef database update
    

内容的提问来源于stack exchange,提问作者Chabba Mohamed Nadir

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 10:37:06