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

如何将Visual Studio for Mac项目连接到Azure Data Studio中的数据库?

解决方案

1. 修正AppSettings.json连接字符串

你当前连接字符串的错误点:使用sa账号的SQL Server身份验证场景下,不能配置Integrated Security=True(该参数是Windows系统专属的集成身份验证开关,Mac环境不支持),同时需要确认数据库名、密码和你实际的配置一致。
修改后的完整配置参考如下:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Error"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "CodeCamp": "Data Source=localhost;User Id=sa;Password=你启动Docker容器时设置的SA密码; Initial Catalog=你实际创建的数据库名称;Connect Timeout=30;"
  }
}

注意修改两个参数:

  • Password字段替换为你启动Azure SQL Edge容器时设置的SA_PASSWORD,和Azure Data Studio连接数据库用的密码一致即可
  • Initial Catalog字段替换为你实际创建的数据库名,如果你确认数据库名称为localhost直接填localhost即可

2. Visual Studio for Mac项目关联配置

  • 打开项目的NuGet包管理器,安装三个依赖包:Microsoft.EntityFrameworkCore、Microsoft.EntityFrameworkCore.SqlServer、Microsoft.EntityFrameworkCore.Design
  • 自定义DbContext类(根据你的业务实体调整即可),示例如下:
public class CodeCampContext : DbContext
{
    public CodeCampContext(DbContextOptions<CodeCampContext> options) : base(options)
    {
    }
    
    // 这里添加你的业务实体对应的DbSet,示例:
    // public DbSet<Article> Articles { get; set; }
}
  • 打开Program.cs文件,注册DbContext服务,新增代码如下:
var builder = WebApplication.CreateBuilder(args);
// 新增这行注册服务,注意替换成你自己的DbContext类名
builder.Services.AddDbContext<CodeCampContext>(options => 
    options.UseSqlServer(builder.Configuration.GetConnectionString("CodeCamp")));
// 保留你原有其他服务注册代码
var app = builder.Build();
// 保留你原有中间件配置代码
app.Run();

3. 执行数据库更新命令

  • 在Visual Studio for Mac的终端中切换到项目根目录,先执行命令生成迁移文件:dotnet ef migrations add InitialCreate
  • 迁移文件生成无报错后,执行目标命令即可完成数据库更新:dotnet ef database update

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 07:36:04