I've created an Console application project then add new App.conf file into my project. In my configuration file, I copied connection string that I've created by adding entity framework as below:
<connectionStrings>
<add name="DBEntities" connectionString="metadata=res://*/DBModel.csdl|res://*/DBModel.ssdl|res://*/DBModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=MY-LAPTOP;Initial Catalog=TestDB;User ID=test;Password=123123;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
On the program.cs I want to using ConfigurationManager class to retain the connection string but always raise me errors.
String connString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
Please suggest me solutions. Thanks in advance.
Connection strings can be stored as key/value pairs in the connectionStrings section of the configuration element of an application configuration file. Child elements include add, clear, and remove.
In ASP.NET Core the configuration system is very flexible, and the connection string could be stored in appsettings. json , an environment variable, the user secret store, or another configuration source.
Your connection string is called DBEntities
Use this line in your code instead:
String connString = ConfigurationManager.ConnectionStrings["DBEntities"].ConnectionString;
This is only a guess since you didn't include the actual exception in your question:
Add a reference to System.Configuration
to your project.
Also, your name-attribute's value is DBEntities
, but you are trying to access ApplicationServices
in your code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With