在 VS Code 中,我使用实体框架创建了一个 .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)
{
"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": "*"
}
builder.Services.AddDbContext<ApplicationDbContext>(options =>
{
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"));
});
Microsoft.AspNetCore.OpenApi 8.0.8
Microsoft.EntityFrameworkCore.Design 8.0.8
Microsoft.EntityFrameworkCore.SqlServer 8.0.8
Microsoft.EntityFrameworkCore.Tools 8.0.8
hostname
验证了这一点)。我启用了 TCP/IP,但不起作用。
我尝试将数据源设置为
KHASHAYAR\\SQLEXPRESS
,但没有成功。
我也尝试了
KHASHAYAR\\khash\\SQLEXPRESS
,但也没成功。
我安装了必要的工具:
sudo apt-get update
sudo apt-get install mssql-tools unixodbc-dev
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc
但是还是没有成功。我需要帮助才能使更新命令正常工作
作为解决方案,我建议使用更新命令并直接指定连接字符串:
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"'
dotnet ef database update --startup-project {project with appsettings}