This one bit me good, and cost quite a bit of time before I figured it out. Therefore, I thought it best to document my findings in case it comes up again.
The Issue
As with many bugs, the behavior didn’t show up until the application was deployed – it worked fine in the development environment. My app uses Linq-to-SQL, and includes an IDE-generated DBML file to interface with my database. The app was connecting to my local database just fine. However, when the app was deployed, and all the changes to the connection string in the web.config file were updated, the connection to the database failed with the error “A network-related or instance-specific error occurred while establishing a connection to SQL Server”. What??
The Solution
After digging and googling, I discovered something about the IDE behavior when it comes to Linq-to-SQL and DBML generation. When the DBML file is first created, the IDE inserts an entry in the Settings.settings file, which contains the full connection string details. Since this connection string matches the settings in my web.config and the app.config for my local projects, everything works fine. The problem is masked by the fact that both the settings file entry and the config file entries are the same. So, to fix this requires first changing the settings on the DBML file to not use the connection string in the Settings.settings file.
After that, the constructor for the repository needs to be updated to use the configuration manager to pull in the connection string from the config file, like this:
Once this is done, the app will pull in the connection string from the config file, and no more error!



[...] Original post from http://www.sunergeosystems.com/2011/03/23/dbml-and-connectionstring-pitfall/ [...]