Connection string with ASP.Net - c#

I have a solution with three projects:
A web application (WA)
A Data Model Layer (DML) using code first, and
A Data Access Layer (DAL)
The WA’s web.config and the DML's app.config each have a connection string section specifying a connection string for the database.
I've noticed that the DML connection string doesn't really matter. So is it safe to completely remove this section from the app.config file?
Also, why is this string unused? Finally, I'm guessing when the application is run, the connection to the database is established using the connection string in the web.config file and the database definition is managed by the DML/DAL?
Is this because the other projects aren’t really being run per se, just the methods and properties are being referenced? Thanks

Yes, you can remove connection string from DML's app.config.
It is unused because you are starting the web application and only it's config does matter at runtime.

Everything needs to go into your app.config / web.config. The config files on the library project only help you to keep track of things that need to go into your app.config

Related

ASP.NET and C#: connection string in web.config vs connection string stored on Azure

I have a web form developed in ASP.NET and C#. I am storing the connection string to a database in the web.config file like this:
<connectionStrings configSource="MySecrets.config" />
This points to a local file in the same directory as the solution. Debugging locally works, however it is not advisable to commit this file to source control to avoid exposing these secrets.
This article mentions that it is possible to store connection strings on Azure - in the Configurations section of an App Service. The article also says that it's possible to retrieve the connection strings in the code by doing:
dbConn = System.Configuration.ConfigurationManager.AppSettings("myConnStringName")
The article also mentions that "if the application setting(s) happen to already exist in your web.config file, Windows Azure Web Sites will automatically override them at runtime using the values associated with your website. Connection strings work in a similar fashion.
(This assumes that your connection strings are explicit in the web.config file, and if committed to source control, they would be exposed.)
However, in my code, I already have a line with:
dbConn = WebConfigurationManager.ConnectionStrings["myConnStringName"].ConnectionString
Questions:
1) How am I supposed to reconcile these two lines without declaring the same variable (dbConn) twice?
2) How can I not commit MySecrets.config to source control, but at the same time use it when I debug my app locally, while using the connection string stored on Azure when working with the published app?

No connection string named 'CREntities' could be found in the application config file

I have 2 web-services. The first has a wsHttpBinding binding, the second is the wsDualHttpBinding. Also there is a project that hosts these web services.
In both services I use EF 6.0. In the service with binding wsHttpBinding, everything works fine. In the service with binding wsDualHttpBinding when accessing the server, an error occurs - "No connection string named 'CREntities' could be found in the application config file."
What am I doing wrong?
EDIT
I read that the connection string should be added to the hosting project, but then why does everything work to bind wsHttpBinding? I've added a connection thread to the host project, but this did not help, there is an error in the lack of the EF library.
"No connection string named 'CREntities1' could be found in the application config file."
That is correct. You need to fix it.
You can either pass in a value different from your default to the context constructor, or you can rename your connection string from "CREntities" to "CREntities1" in your config file.
That said, in the future, please don't post pictures of code, post code.
I'm so dumb that I just do not have words. No project for hosting needs to be created, Visual Studio will customize everything if you created the WCF Aplication project. A good example of how this works is here: https://www.youtube.com/watch?v=VA4r-iYCPQY&t=819s
I'm sorry that I wasted your time, I hope someone will need it. (No)

connectionstring declare in iis without declaring in web.config

I want to declare my connection string in IIS and get it from there. I don't want to declare it in web.config page. Rather I need to know if is it possible to get the string from iis in web.config or read it from code file. I am using asp.net 4.0.,coding in c# and server is IIS7.5
According to this article you can use the IIS UI, or the command line to modify your connection string, but this will just write into the <connectionString> element in the web.config file any way (unless you've set it up to save elsewhere).
Also, you can store it in another .config file if you wish, and pull it into your web.config like so
<appSettings file="../VirtualDirectory/config/Env.config">
</appSettings>
You could then call it like so in your code:
System.Configuration.ConfigurationManager.AppSettings["DefaultConn"]
This can be quite useful if you want the location of your connection string to not be under your site (i.e. in a virtual directory).
You should put your connection string in the web.config. Putting your connection string elsewhere might not be portable, like moving from machine to machine. You also have the machine.config
In all, put in the web.config and you can encrypt it if you care about the secrecy
You can also have a separate config file e.g. db.config referenced from your web.config using the configSource attribute.
<connectionStringss configSource="Configuration\db.config" />

Issues on dynamically change and switching the connectionString in web Config

I am trying to switch different databases for a web application at Run time.
Senario
We have one asp.net web application and different databases for different customers.I am trying to switch particular connection string value from a common database where i am keeping a mapping table for connection string ,particular customer id and password .After the successful lo gin i am piking a connection string from the common database and edit the web.config file connection string section by replacing selected connection string at run time.
i am doing this by add following code to login event
conectionString = cString;
Configuration openWebConfiguration = WebConfigurationManager.OpenWebConfiguration("~");
ConnectionStringsSection sections = openWebConfiguration.GetSection("connectionStrings") as ConnectionStringsSection;
if (sections != null)
{
sections.ConnectionStrings["ConnectionStringName"].ConnectionString = conectionString;
ConfigurationManager.RefreshSection("ConnectionStringName");
openWebConfiguration.Save();
}
i am reading above connection string on a page by using ConfigurationManager.problem is the web config file is changing but after calling to another page using Response.Redirect will throw an exception .Exception is "Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack "I can realized this is something happen on cross threaded environment.My questions are
What is exact reason for above exception?
which page life circle of the Asp.net reads the setting from the web config file ?
What is the proper way i can implement above scenario?
I am wondering why this question seems still unanswered.OK i have found some answermy self.It may be wrong but some how they are giving some meaning to me.I assume following are acceptable for my knowledge level.
1)I don't know the exact reason ,but this is something happen because of code is modified while an application running
2)Based on my search WEB Config file is started to read by the application when the IIS server start.So what ever values to be modified inside WEB Config ,require to restart the IIS server to load them in to memory.We can modify the connection string dynamically but still the application will run on the previous connection string.So we need t restart the IIS to load newer one.
Note:Modify a existing connection string is different than add a new connection string to a WEB Config.
3)I have used a common data Base where i have authentication details for different different connection strings for Several database.WeB config has the connection string for above master database.If an user gives his authentication detail it will select his connection string and load it as new connection string .So the remaining process will be based on that connection string.
Any new arguments for above answers are highly appreciable.I need corrections from other developers because i am very eager to learn.

C# ConnectionString for EF Repository Class Library

I have a database and a C# class library which acts as a repository for accessing objects from the database using Entity Framework. When trying to use this repository assembly in other projects I am running into difficulty; My working assumption is that this it due to the Connection String as I am getting the following error:
The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.
I have added an App1.config file to the project and added the same connection string I had successfully used when creating the database:
<add name="EDSEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SQLite;provider connection string='data source="C:\Documents and Settings\hmay\My Documents\My Projects\Timetabler\DataSets\EDS.db";foreign keys=true'" providerName="System.Data.EntityClient" />
.... but it doesn't work. I don't understand the connection string very well and wondered if there is something else here I might need to tweak?
Regards
Adding app1.config would not do anything. App.config and web.config are specially named files and they need to be named exactly that. However if you use your library in multiple apps i would refactor it.
Change the DbContext class to have a single constructor that takes a connection string and push the responsibility for creating the connection string into the app rather than the library. It is not good to have a class library that expects your app to have a particular named entry in the connection strings section of its config.
try trimming metadata references to metadata=res://*

Categories