【发布时间】:2020-05-14 03:58:26
【问题描述】:
我一直在尝试使用 EFCore 和 .net 核心来学习几个不同的教程,但在尝试创建本地数据库时我完全被阻止了。
我同时使用了 powershell 工具和命令行工具来尝试创建初始迁移(或做任何事情,真的)。
我一直收到错误:
System.InvalidOperationException: An exception has been raised that is likely due to a transient failure. Consider enabling transient error resiliency by adding 'EnableRetryOnFailure()' to the 'UseSqlServer' call.
---> Microsoft.Data.SqlClient.SqlException (0x80131904): A connection was successfully established with the server, but then an error occurred during the login process. (provider: Named Pipes Provider, error: 0 - No process is on the other end of the pipe.)
系统上当前不存在该数据库,但本地 SQL Server 似乎已启动并正在运行。
这是添加上下文的c#代码:
services.AddDbContextPool<TestDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("TestDb")
)
);
这是连接字符串代码:
"TestDb": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=TestDb"
无论我运行add-migration、dotnet ef migration add 还是dotnet ef dbcontext info,都会遇到类似的错误。 (注意:在 dotnet 调用中,我使用的是 -s ..\{webproject}\{webproject}.csproj 属性
我还通过添加Trusted_Connection=True; MultipleActiveResultSets=True; 和Integrated Security=true 的各种组合来弄乱连接字符串。
我已进入 SSMS 并确保服务器身份验证为 SQL Server 和 Windows 身份验证模式,并且最大连接数设置为 0(无限制)。我也去登录并尝试将用户添加到几乎所有的服务器角色。
所以,是的,我很困惑。我已经使用 EF 多年了,尽管这是我第一次使用 EFCore,而且我绝对是一名开发人员,而不是一名 SQL 管理员。这也是我第一次尝试在这台特定的计算机上使用本地数据库。
编辑:查看 AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\mssqllocaldb 中的 error.log 我看到了这个错误:
2020-01-28 10:15:03.50 Logon Error: 18456, Severity: 14, State: 38.
2020-01-28 10:15:03.50 Logon Login failed for user 'LAPTOP-NC6HQ4TB\ripli'. Reason: Failed to open the explicitly specified database 'TestDb'. [CLIENT: <named pipe>]
这令人困惑。当然我不能打开指定的数据库。重点是我想创建一个尚不存在的数据库。
【问题讨论】:
-
您可能没有安装 LocalDB(这是您正在使用的连接字符串)。 docs.microsoft.com/en-us/sql/database-engine/configure-windows/…
-
所以在 SSMS 中我能够连接和操作 (LocalDB)\MSSQLLocalDB。我可以从那里创建数据库并修改角色和设置。我是否缺少一些细微差别?
-
将您的 DataSource 值更改为
.并查看错误是否消失 -
这会将错误消息更改为:
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: 26 - Error Locating Server/Instance Specified) -
@Neil 我尝试安装单独的 SQL Server Express 实例,但我仍然遇到同样的问题。
标签: c# sql-server entity-framework .net-core entity-framework-core