views:

415

answers:

2

Hello,


1) By default configuration elements have their connectionStringName attribute set to LocalSqlServer, and as far as I know, this attribute refers to connection defined in the element in machine.config file.

a) I assume this connection string refers to database aspnetdb.mdf?!

b) I understand aspnetdb.mdf is used in cases where we don’t manually create membership or profile database ( by calling *aspnet_regsql* ), but I still don’t understand the purpose of configuration elements having connectionStringName attribute set to LocalSqlServer set? Namely, when and why would they need to access this database?

c) What happens if we manually set membership database via *aspnet_regsql* and thus don’t use aspnetdb.mdf? How will configuration elements know that we’re not using aspnetdb.mdf and thus instead try to access database we created?


2) If we wanted LocalSqlServer entry from machine.config to point to some other database file, we could do the following:

      <connectionStrings>
        <remove name="LocalSqlServer" />
        <add name=”LocalSqlServer” ...  />
      </connectionStrings>

I understand that the purpose of <remove> element is to cancel any previously declared elements with same name, but in above example we simply changed the attribute of already existing connection, and as such machine.config doesn’t have two connections with same name, so why did we have to include <remove> element?


thanx

+1  A: 

The convention used is one of many that could have been chosen to accomplish the same task, but the people who were on the ASP.NET team at MS at the time are really the only ones who can say "why" that set of conventions was used. My understanding is that the purpose of the current configuration is to make it as easy as possible for a beginner to get started. i.e. run some wizards, automatically generate database with preconfigured settings, drag-n-drop a few security controls and they have something to work with. Since this was designed for beginners, more experienced developers run into the same set of questions that you're having now because digging into how the pieces fit together isn't simple.

One of the things you'll notice in machine.config is that all of the providers (Membership, Roles, Profile, etc) use this LocalSqlServer connection string name, which again supports the beginner scenario. Therefore, to use youur own database, you need to remove the default definition of LocalSqlServer and define your own. There isn't a replace element in the config file definition, so you have to use the remove/add sequence, which is the logical equivalent. By changing the connection string and leaving its name as LocalSqlServer, all of the providers in machine.config get pointed at your DB. This gives you the default provider definitions in for your database.

Now, if you wanted to customize the provider definitions, you could add them to your own Web.config and change their settings. At that point, you could leave LocalSqlServer as the connection string for the custom provider definitions or you could create your own connnection string and then point your custom provider definitions at your own connection string and you won't need to worry about LocalSqlServer anymore. If you remove LocalSqlServer from you web.config, you'll need to add custom provider definitions to your own web.config that reference your database string.

Hope this helps,

Joe

May I ask why <remove> element was necessary in the above code? After all, we just changed the attribute, so I don't see the reason for <remove>? Also, why do configuration elements need to access database and thus require attribute pointing to connection string?
SourceC
+1  A: 

From the connectionStrings element article on MSDN:

Connection strings that are contained in a parent configuration file are inherited, unless the clear element is used in the child configuration file. The following default connectionStrings element is configured in the Machine.config file. Copy Code

<connectionStrings>
    <add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated
        Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User
        Instance=true" providerName="System.Data.SqlClient" />
</connectionStrings>

So if that section of the config file is not modified, it automatically has that connection string.

GoodEnough
But <add name="LocalSqlServer"> element in my original post was modified inside machine.config, thus <remove> shouldn't be necessary, since there isn't any parent configuration file above machine.config?
SourceC
I guess <remove> wouldn't be necessary. My guess is that elements that refer to LocalSQLServer are just referring to it by default. The default connection string is just there in case someone forgets to remove it. I guess it's good practice to remove it from other configuration files.
GoodEnough
Ok, so I gather <remove> was not necessary in code I've posted in my initial post?! thank you all for your help
SourceC