Nov 7, 2009 at 7:18 AM
Edited Nov 7, 2009 at 9:36 AM
I am on my early stages of learning ASP.NET MVC and I came across an issue that proved somehow difficult for me to solve.
The Data Access layer is built using Linq to Sql classes and the database is represented by an instance of SQL Server 2005 (not SQLEXPRESS, jut to be clear!). I have chosen to put the DAAB in its separate config file and I have created the necessary connection
string for my SQL Server instance. "SelectedSource" property for Configuration Sources block is set to System Configuration Source. Version of Enterprise Library is 4.1.
Now I want to initialize the database connection at run time using the DefaultDatabase connection string.
I need the DefaultDatabase connection string to initialize my DataContext descendent that was created by the LINQ to SQL editor.
I have used several approaches but they all have failed. EntLib 4.1 docs are not very descriptive about this particular task of mine, although I suspect the task to be not so uncommon. At the current moment I am stuck with the following code:
public string DefaultConnectionString
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = "dataconfiguration.config";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
ConnectionStringsSection connStringsSection = config.ConnectionStrings;
ConnectionStringSettings connStringsSettings = connStringsSection.ConnectionStrings["DefaultDatabase"];
This property returns the empty string so obviously I am not reaching to my own connection string. The variable connStringsSettings only contains the default LocalSqlServer connection string. (Side note: Why is EntLib editor so stubborn about placing DAAB
in both App.config and DataConfiguration.config when I clearly delete the block from App.config??)
Has anyone had a similar task that solved succesfully?