【问题标题】:Entity Framework code first approach connection string configuration实体框架代码优先方法连接字符串配置
【发布时间】:2017-09-11 06:40:51
【问题描述】:

我已经对我现有的 asp.net 网络表单应用程序实施了身份和 OWIN 身份验证。我提到的网站: https://docs.microsoft.com/en-us/aspnet/identity/overview/getting-started/adding-aspnet-identity-to-an-empty-or-existing-web-forms-project

我在使用实体框架代码优先方法的连接字符串时遇到问题。我参考了该网站:http://odetocode.com/Blogs/scott/archive/2012/08/14/a-troubleshooting-guide-for-entity-framework-connections-amp-migrations.aspx 寻求帮助。但它们都不起作用。

我使用 VS 2015、SQL Server 2014 和 Windows 10 操作系统。

对于 SQL Server 身份验证(Windows 身份验证),我的服务器名称是:DESKTOP-07C2G55

我按照网站上的说明更改了我的连接字符串,但它们都不起作用。我的第一个连接字符串是::

<connectionStrings>
    <add name="DefaultConnection" 
         connectionString="Data Source=DESKTOP-07C2G55;Initial Catalog=WebFormsIdentity;AttachDbFilename=|DataDirectory|\WebFormsIdentity.mdf;Trusted_Connection=Yes;Integrated Security=True" 
         providerName="System.Data.SqlClient" />
</connectionStrings>
<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
        <parameters>
            <parameter value="v13.0" />
        </parameters>
    </defaultConnectionFactory>
    <providers>
        <provider invariantName="System.Data.SqlClient" 
                  type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
</entityFramework>

在我尝试注册任何用户时使用此连接字符串时出现以下错误:

文件“c:\users\myPC\documents\visual studio 2015\Projects\RENTAL\RENTAL\App_Data\WebFormsIdentity.mdf”的目录查找失败,出现操作系统错误 5(拒绝访问。)。

创建数据库失败。无法创建列出的某些文件名。检查相关错误。

说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.Data.SqlClient.SqlException:操作系统对文件“c:\users\myPC\documents\visual studio 2015\Projects\RENTAL\RENTAL\App_Data\WebFormsIdentity.mdf”的目录查找失败错误 5(访问被拒绝。)。

创建数据库失败。无法创建列出的某些文件名。检查相关错误。

然后我在我的 VS 控制台中运行以下命令。我明白了:

PM> Get-Service | Where-Object {$_.Name -like '*SQL*'}

Status   Name               DisplayName                           
------   ----               -----------                           
Running  MSSQLFDLauncher    SQL Full-text Filter Daemon Launche...
Running  MSSQLSERVER        SQL Server (MSSQLSERVER)              
Running  MSSQLServerOLAP... SQL Server Analysis Services (MSSQL...
Stopped  SQLBrowser         SQL Server Browser                    
Stopped  SQLSERVERAGENT     SQL Server Agent (MSSQLSERVER)        
Running  SQLWriter          SQL Server VSS Writer 

PM> SqlLocalDb info
MSSQLLocalDB

然后我更改了连接字符串,将数据源更改为Data Source=MSSQLLocalDB

<connectionStrings>
    <add name="DefaultConnection" 
         connectionString="Data Source=MSSQLLocalDB;Initial Catalog=WebFormsIdentity;AttachDbFilename=|DataDirectory|\WebFormsIdentity.mdf;Trusted_Connection=Yes;Integrated Security=True"  
         providerName="System.Data.SqlClient" />
 </connectionStrings>

我得到了错误

键“attachdbfilename”的值无效。

说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.ArgumentException:键“attachdbfilename”的值无效。

我再次将连接字符串更改为Data Source= .\SQLEXPRESS

<connectionStrings>
    <add name="DefaultConnection" 
         connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=WebFormsIdentity;AttachDbFilename=|DataDirectory|\WebFormsIdentity.mdf;Trusted_Connection=Yes;Integrated Security=True"  
         providerName="System.Data.SqlClient" />
 </connectionStrings>

我得到了错误:

与 SQL Server 建立连接时出现与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供者:SQL 网络接口,错误:26 - 错误定位服务器/指定的实例)

说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.Data.SqlClient.SqlException:建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供者:SQL 网络接口,错误:26 - 错误定位服务器/指定的实例)

我无法弄清楚问题的原因是连接字符串还是代码中的其他任何地方。在其他解决方案中,提到了编辑 DBContext 类,但在教程中没有创建这样的类。我没有使用 MVC,尝试实现 Identity 和 OWIN 模块的简单 Web 表单应用程序。请帮我。

【问题讨论】:

标签: c# asp.net sql-server entity-framework


【解决方案1】:

您的 PowerShell 脚本的输出显示您有一个正在运行的 SQL Server 默认实例(“实例”名称为 MSSQLSERVER):

Running  MSSQLSERVER        SQL Server (MSSQLSERVER)              

这意味着,您可以使用Data Source=.(或Data Source=(local))作为您的服务器名称(您不得定义实例名称以连接到默认实例),在Initial Catalog 中定义数据库名称,并一劳永逸地丢弃AttachDbFileName 废话(并让SQL Server 处理所有与文件相关的来龙去脉——不要乱来) .mdf自己的数据库文件!):

<connectionStrings>
    <add name="DefaultConnection" 
         connectionString="Data Source=.;Initial Catalog=WebFormsIdentity;Trusted_Connection=Yes;Integrated Security=True"  
         providerName="System.Data.SqlClient" />
 </connectionStrings>

【讨论】:

  • 仍然遇到同样的错误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: 50 - Local Database Runtime error occurred. The specified LocalDB instance does not exist. )。请帮帮我!!!
  • 我也面临同样的产品。您找到解决方案了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-31
  • 2015-04-09
  • 2011-09-28
  • 2012-03-03
  • 1970-01-01
  • 1970-01-01
  • 2011-08-25
相关资源
最近更新 更多