【发布时间】:2021-09-16 19:37:12
【问题描述】:
我是 Blazor 和 EF 核心的新手。当我在控制台中尝试更新数据库时,我收到了这条消息。我已经安装了所有 EF 核心包,在过去的 9 个小时里,我一直在尝试找出解决方案。
//appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"DbConnection": "server= Assignment; database=InterviewAssignment;Trusted_Connection=True;"
}
}
//AppDBContexts.cs
namespace InterviewAssignment.Data
{
public class AppDBContext : DbContext
{
public AppDBContext(DbContextOptions<AppDBContext> options) : base(options)
{
}
public DbSet<Contact> Contacts { get; set; }
}
}
//Method in Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddSingleton<WeatherForecastService>();
services.AddScoped<EmployeeService>();
#region Connection String
services.AddDbContext<AppDBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DbConnection")));
#endregion
}
first sentence of the ERROR
Microsoft.Data.SqlClient.SqlException (0x80131904): 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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
【问题讨论】:
-
这通常是由于连接字符串错误。您可能希望在 appsettings.json 文件中验证这一点。如果它是远程服务器,那么您可能需要重新验证证书。
-
您真的有一个数据库服务器在名为“Assignment”的主机上运行吗?公司connectionstrings.com/sql-server(只是看几个例子)
-
是的,我有,因为那是另一个线程上的解决方案。我还尝试通过管理重新启动 SQL 服务器,并确保它具有允许它工作的所有属性。
标签: c# .net entity-framework-core blazor-server-side