【问题标题】:EntityFramework connect to Visual Studio SQL Server (localdb)EntityFramework 连接到 Visual Studio SQL Server (localdb)
【发布时间】:2021-10-25 03:20:15
【问题描述】:

我正在通过 Visual Studio 运行 Sql Server 的本地实例:

它是在线的,我可以通过 UI 查看数据库中的数据。

我的 App.config 文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
    <configuration>
   <configSections>
   <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
  </configSections>

<connectionStrings>
    <add name="EntitiesContext" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" providerName="System.Data.SqlClient"/>
    <add name="DefaultConnection" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" providerName="System.Data.SqlClient" />
</connectionStrings>


<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
        <parameters>
            <parameter value="mssqllocaldb" />
        </parameters>
    </defaultConnectionFactory>
    <providers>
        <provider invariantName="System.Data.SqlClient" 
                  type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
</entityFramework>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/></startup> 
</configuration>

我的 EntitiesContext 如下所示:

有人可以帮助解决我所缺少的问题,因为这会导致与以下内容的连接失败:

System.Data.SqlClient.SqlException: '建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供者:命名管道提供者,错误:40 - 无法打开与 SQL Server 的连接)'

内部异常 Win32Exception: 系统找不到指定的文件

【问题讨论】:

标签: entity-framework visual-studio-2019 dbcontext localdb


【解决方案1】:

问题是您正在使用的连接字符串正在恢复为命名管道连接,它告诉您该管道上没有任何内容正在侦听。

如果您的本地数据库设置为共享内存连接,则将其更改为“localhost\MSSQLLocalDB;....”应该可以工作。

要检查您的服务器上启用了哪些协议,您可以运行 MMC.exe,然后为 SQL Server 配置管理器添加插件。在 SQL Server 网络配置下,它将显示您的 SQL Server 实例是否允许命名管道或 TCP/IP。我相信默认情况下 SQL Express 只启用共享内存,这对于本地测试目的来说很好。

【讨论】:

    猜你喜欢
    • 2014-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-03
    • 2015-09-05
    • 1970-01-01
    • 2022-01-22
    相关资源
    最近更新 更多