sql-server - dotnet ef 数据库更新中与网络相关或特定于实例的错误

标签 sql-server asp.net-mvc entity-framework asp.net-core

我正在学习使用 Visual Studio 构建 ASP.NET Core MVC 应用程序 this教程。在“Adding a model ”步骤中,我创建了一个新的单独项目(如说明中所述),但是当我运行 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: SQL Network Interfaces, error: 52 - Unable to locate a Local Database Runtime installation. Verify that SQL Server Express is properly installed and that the Local Database Runtime feature is enabled.)



但是我已经安装了 SQL Server 并且 SQLEXPRESS 服务正在运行。我也尝试打开端口,但没有帮助。

最佳答案

设置本地数据库

模板的连接字符串用于 localdb专门针对 mssqllocaldb实例。确保您拥有两者。

appsettings.json

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-MvcMovie-87ad796f-548e-4862-9713-d2976453319e;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
}

确保您已安装 LocalDB。 要检查,请打开命令提示符并运行 sqllocaldb .这将打印 LocalDB 命令行帮助信息。如果您没有 LocalDB,then install it.

确保您有一个名为 MSSQLLocalDB 的实例. 要检查,请运行 sqllocaldb info .如果您没有该实例,则需要创建它 ( sqllocaldb create mssqllocaldb ) 或更改连接字符串以指向您拥有的实例。

我的机器上的工作示例
dotnet ef database update命令在我的机器上运行。这是在我的提示下各种命令的输出。
> sqllocaldb info
MSSQLLocalDB

> dotnet ef migrations add Initial
Project MvcMovie (.NETCoreApp,Version=v1.0) will be compiled...
Compiling MvcMovie for .NETCoreApp,Version=v1.0
Compilation succeeded.
    0 Warning(s)
    0 Error(s)
Time elapsed 00:00:01.7783361

Done. To undo this action, use 'dotnet ef migrations remove'

> dotnet ef database update
Project MvcMovie (.NETCoreApp,Version=v1.0) was previously...
Done.

关于sql-server - dotnet ef 数据库更新中与网络相关或特定于实例的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38638757/

相关文章:

c# - 使用存储库模式时如何取回数据库生成的 ID?

c# - Entity Framework Scaffold-DbContext 用户登录失败

entity-framework - 如何在 Entity Framework 核心中重命名迁移

sql - 从 SQL Server 中的表的每一行中获取单词 `href` 和 `nofollow` 的计数

c# - 触发器返回一个结果集和/或在 SET NOCOUNT OFF 的情况下运行,而另一个未完成的结果集处于事件状态

c# - 如何在 MVC 中为双因素身份验证电子邮件设置模板

c# - 对于 Android,Request.Browser.IsMobileDevice = false,为什么?

sql-server - SQL Server 管理工作室 : Open table for Editing through keyboard shortcut

sql - 根据日期创建具有不同值的表

c# - 如何直接从 Jquery 访问 .net MVC ViewData