简体   繁体   中英

EF 6 not using connection string in Web.config

I am receiving the error below which for the life of me I cannot resolve. I am using EF 6 with Identity 2 and it was working fine a few days ago.

I fired up VS 2015 yesterday and started receiving the error both when publishing to the test site and locally. It is almost as if EF is looking for a local instance of SQL Express even though Web.config is configured to use a SQL 2014 Standard server.

Here is the error:

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

This is the connection string and EF config in Web.config:

<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>

I can connect fine to the DB using SQL Server Management Studio with the above connection string.

What should I look for?

The first place to check is your DB context to ensure you have specified the name of the connection string:

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

    // ...

Your connection string looks fine. The error indicates that there is still something in your web.config or code that is looking for App_Data directory which is strange. Have your tried doing a find in your solution for App_Data or |DataDirectory|.

Looks like,There is a reference to the App_data folder somewhere in your code or (LocalDb) or SQlExpress

You don't happen to have a Config Transform file, like Web.Debug.config , or Web.Release.confi g underneath your Web.config file do you? It might be rewriting your connection string.

Usually Config Transform files do their transformations when you publish a site, but I'm not sure how they work in some of the debugging modes, since there are a few modes for ASP projects like IIS Express and Local IIS.

I had it happen to me today with an App.config file, an App.Debug.config was rewriting my connection string, I didn't even notice that there was a App.Debug.config until I expanded the node. It had a totally different connection string.

After much debugging I found a typo in the namespace in Startup.Auth.cs

This meant that no connection string was passed through to DefaultConnectionFactory which intern meant that The connection factory then uses the context name as the database name in a default connection string. (This default connection string points to .\\SQLEXPRESS on the local machine unless a different DefaultConnectionFactory is registered.)

Once the namespace was corrected, everything functioned as planned :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM