c# - EF 6 不在 Web.config 中使用连接字符串

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

我收到以下错误,我一生都无法解决。我将 EF 6 与 Identity 2 一起使用,几天前它运行良好。

我昨天启动了 VS 2015,并在发布到测试站点和本地时开始收到错误。这几乎就像 EF 正在寻找 SQL Express 的本地实例,即使 Web.config 配置为使用 SQL 2014 标准服务器。

这里是错误:

Server Error in '/' Application. 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) Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

SQLExpress database file auto-creation error:

The connection string specifies a local Sql Server Express instance using a database location within the application's App_Data directory. The provider attempted to automatically create the application services database because the provider determined that the database does not exist. The following configuration requirements are necessary to successfully check for existence of the application services database and automatically create the application services database:

If the application is running on either Windows 7 or Windows Server 2008R2, special configuration steps are necessary to enable automatic creation of the provider database. Additional information is available at: http://go.microsoft.com/fwlink/?LinkId=160102. If the application's App_Data directory does not already exist, the web server account must have read and write access to the application's directory. This is necessary because the web server account will automatically create the App_Data directory if it does not already exist. If the application's App_Data directory already exists, the web server account only requires read and write access to the application's App_Data directory. This is necessary because the web server account will attempt to verify that the Sql Server Express database already exists within the application's App_Data directory. Revoking read access on the App_Data directory from the web server account will prevent the provider from correctly determining if the Sql Server Express database already exists. This will cause an error when the provider attempts to create a duplicate of an already existing database. Write access is required because the web server account's credentials are used when creating the new database. Sql Server Express must be installed on the machine. The process identity for the web server account must have a local user profile. See the readme document for details on how to create a local user profile for both machine and domain accounts.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)] System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +92 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +285

这是 Web.config 中的连接字符串和 EF 配置:

<entityFramework>
  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  <providers>
    <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
  </providers>
</entityFramework>
<connectionStrings>
  <add name="DefaultConnection" connectionString="Data Source=SQL5017.Smarterasp.net;Initial Catalog=DB_9CF975_moverlive;User Id=xxxxxxxxx;Password=xxxxxxx;" providerName="System.Data.SqlClient" />
</connectionStrings>

我可以使用带有上述连接字符串的 SQL Server Management Studio 连接到数据库。

我应该寻找什么?

最佳答案

首先要检查的是您的数据库上下文,以确保您指定了连接字符串的名称:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", false)
    {
    }

    // ...

关于c# - EF 6 不在 Web.config 中使用连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33266494/

相关文章:

c# - 如何确保在构造函数完成执行之前不调用任何内容

c# - 在 Entity Framework 核心中仅包含相关实体的 ID

c# - 如何在asp.net中打印标签或面板的内容?

sql - 从两个字段中选择第一个不同的值

c# - ASP.NET 和 C# : hide spinning wheel when button has finished processing

c# - 防止 Revit 窗口打开

sql - MySQL GROUP_CONCAT 令人头痛

mysql - 用表的 id 替换字符串值的最后一个字符

asp.net - 使用 NAnt 中的 ASP 编译器构建 ASP .Net MVC 应用程序

asp.net - 如何在 Ext.NET 窗口中放置 GridPanel?