I put an updated version of the WebConfigManager project over on codeplex. http://www.codeplex.com/MyLocalBroadband
I put it out there to correct a mistake I had made based on many of the examples I’ve seen. So I’m guessing this is a widely made coding issue.
If you want to make updates to the web.config via code, I encourage you to use this project outright or check it out as an example.
Here’s the most recent change I’ve made relating to said problem: I updated the code that removes the web.config entries so that it removes modifications of type EnsureSection.
If you’ve done this kind of web.config manipulation before, you may know that once you insert a section (such as <connectionString> ) using the SPWebConfigModificationType of EnsureSection, you cannot remove it from the web.config using the standard webApp.WebConfigModifications.Remove() method. Many people, myself included, took this to mean you shouldn’t call the Remove method on a modification of type EnsureSection.
That is wrong, hence my recent update.
Here’s why:
SharePoint’s web application class contains a collection that stores all the config modifications and their owners. Typically, you loop through this and check the owner of each modification to do your removes. While calling Remove() on an EnsureSection type modification will not actually update the web.config, it WILL, remove it from this collection. So if you want to keep this clean, you should call Remove on all your modifications, regardless of type. It can really become a mess if you activate & deactivate a feature several times. Each activation will add items to the collection that are never removed on the deactivation.
The easiest way to see this problem is using the SharePoint Manager (http://www.codeplex.com/spm) to inspect this collection.