Dofactory.com
Dofactory.com
 Back to list
Views:   8.9K
Replies:  1
Archived

A little confused about connection strings

I'm a little confused on how the connection strings are used in the Patterns in Action 4.0 application.
They have the connection string setting in the Hosting Layer (WCF.ActionServer) and in the App.Config in the Data Layer.
How and why is that and which one is being used?
 
To me it looks like the one in the App.Config is being used and if that's the case why is it also in the web.config in the Hosting layer. Here is what the code looks like in the Db.cs Hosting Layer.

private static readonly string dataProvider = ConfigurationManager.AppSettings.Get("DataProvider");
private static readonly DbProviderFactory factory = DbProviderFactories.GetFactory(dataProvider);

private static readonly string connectionStringName = ConfigurationManager.AppSettings.Get("ConnectionStringName");
private static readonly string connectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;

Thanks
Dennis
Dennis Tucker, Apr 06, 2012
Reply 1
Dennis,

That's an easy one to get confused about, but also and easy one to answer.

When the Entity Framework designer is created and you make a connection to your database in order to add tables to the designer, it saves the connection string to the App.Config file in your data objects project.  It is only used by the EF designer for you to manage the connection with your database.

The connection string in the Hosting layer IS the one that is used for data access by the application. 

As you have shown in the code you present, the DataObjectFactory class in the DataObjects project looks for an appSettings.config file with a setting called "ConnectionStringName".  It's this name that will point to one of the available connectionstrings in the "connectionStrings" section of that web.config file.

So the bottom line is that the connection string from the web.config file in the WCF hosting layer is the one that is used.

I hope this answers your question.

King Wilder
King Wilder, Apr 15, 2012
Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Cookies       Do Not Sell       Licensing      
Made with    in Austin, Texas.  - vsn 44.0.0
© Data & Object Factory, LLC.