【问题标题】:C# Blazor and EF core Update-database errorC# Blazor 和 EF 核心更新数据库错误
【发布时间】: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


【解决方案1】:

正如 cmets 中的其他人所提到的,这很可能是由于您的连接字符串中的服务器名称。通过打开 SQL Server Manager 来测试它的最简单方法,但如果您没有安装它,请在 Visual Studio (2019) 中按 Ctrl-Alt-S,它将打开服务器资源管理器。在那里你会找到你需要使用的服务器名称。

示例 1:SQL Server 管理器

示例 2:服务器资源管理器

这应该为您提供服务器名称。然后你也可以用InitialCatalog替换Database

【讨论】:

  • “数据库”和“初始目录”(是的,有空格)是entirely synonymous。没有必要提倡用另一个替换一个
【解决方案2】:

错误提示“命名管道提供程序 - 确保在 SQLServer 配置管理器工具中启用命名管道,否则它将无法工作。还要确保在正确的实例/客户端上启用它,即您的防火墙为相关端口打开并且您的连接字符串使用正确的服务器名称(和实例名称)

【讨论】:

  • 已经这样做了。不过谢谢!
  • 你能用例如SSMS连接到它吗?
猜你喜欢
  • 2020-04-14
  • 2017-04-22
  • 2020-06-14
  • 1970-01-01
  • 2021-10-05
  • 1970-01-01
  • 2017-09-09
  • 2023-01-24
  • 2020-01-22
相关资源
最近更新 更多