Tuesday, December 29, 2009

Nested web.config files and connectionStrings (entry has already been added)

When using the Entity Framework (EF) recently, I ran into a situation where I received the following error message:
The entry 'myConnectionstring' has already been added...web.config line: 47
After investigating I found out this was due to my use of web.config files in nested directories. Here is the situation...

I have a reusable EF DAL (data access layer) that is used by several web services on a single web site. Several of these web services are in sub-folders with their own web.config files. This allows me to have custom settings for each service as well as being able to easily xcopy an entire directory and know I have all the correct settings. For the EF I have a connectionStrings key in the config files. In addition to the nested web.config files, I have the top-level config which also has the EF connectionStrings key.

When I tried to access a web service I would get the error above because .NET was merging the web.config files and found duplicate connectionString keys. To fix this I just had to add the following element within the connectionStrings section, directly before my key:
<remove name="myConnectionString" />
To facilitate knowing I can easily xcopy any web service, it is good practice to add this to all the web.config files, even the top-level one.

No comments:

Can't RDP? How to enable / disable virtual machine firewall for Azure VM

Oh no!  I accidentally blocked the RDP port on an Azure virtual machine which resulted in not being able to log into the VM anymore.  I did ...