views:

516

answers:

3

Hello, I'm trying to get data from app.config and I always get zero. The App.config is here:

    <?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="ExplorerContext" connectionString="metadata=res://*/ExplorerData.csdl|res://*/ExplorerData.ssdl|res://*/ExplorerData.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=MYT\SQLEXPRESS;Initial Catalog=Explorer;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

Could someone explain what is wrong, why I cannot get the values, System.Configuration.ConfigurationManager.AppSettings.Count is always 0

I fogot to specify that I use class library, that I'm trying to check using NUnit project. And this class library calls one more project (class library too) that uses ADO.NET Entity Project.

Thanks

+5  A: 

You're not using AppSettings! Check ConfigurationManager.ConnectionStrings instead.

Anthony Pegram
+1  A: 

You do not have any AppSettings declared in your config file. If you are trying to get the Connection strings then you should use:

var connectionString = ConfigurationManager.ConnectionStrings["ExplorerContext"].ConnectionString;

EDIT If you are using NUnit, you can supply a configuration file as long as it is named after the DLL. For example, if the DLL is named Foo.dll, then the configuration file must be named Foo.dll.config. Visual Studio will not do this for you for a DLL. You must create the file manually and you must ensure it gets into the proper bin folder.

See NUnit Configuration Files for more.

Thomas
Oh, my!When I use this construction I can see some strange connection string that I DON'T have in my app.config:System.Configuration.ConfigurationManager.ConnectionStrings[0]{data source=.\SQLEXPRESS;Integrated ... Name: "LocalSqlServer" ProviderName: "System.Data.SqlClient"I cannot imagine where is it from???
Seacat
I didn't specify ConnectionStrings[0], I pulled it by name using the name in your OP (i.e. ConnectionStrings["ExplorerContext"]. By default, ASP.NET does add a connection string in the machine.config file for LocalSqlServer which you can ignore.
Thomas
@user46503: that connection string to the local SQL server is from your machine.config deep in the bowels of your system.....
marc_s
When I try to call System.Configuration.ConfigurationManager.ConnectionStrings["ExplorerContext"] it always returns null! I cannot understand, why because I can see App.config and it copied into bin/debug folder...
Seacat
I've updated the root post. Please look.
Seacat
A: 

I forgot to specify that I use class library, that I'm trying to check using NUnit project. And this class library calls one more project (class library too) that uses ADO.NET Entity Project.

You need to put your configuration information into the main app - the app using/calling your class library project with the EF model. .NET configuration does not natively support class library level app.config's.

So in your test environment, the main test harness will need to have these entries in its app.config.

If you insist that your class library assembly have its own config - check out Jon Rista's Cracking the Mysteries of .NET 2.0 Configuration where he explains in great detail how to use the ConfigurationManager.OpenExeConfiguration call to open any arbitrary *.config file and use it within the .NET 2.0 configuration system. It works - but it's more work and I wouldn't really recommend it.

marc_s