Unfortunately, by default, they are added to your Settings.cs file which is then compiled away into your code. This does the following:
- reduces your ability to easily configure your applications for different environments (ie you would need to have different compiled dlls for dev, test and production).
- It is also LESS secure because you can easily use reflection to examine your dlls for passwords - whereas you can encrypt the web.config so it is only viewable to people who have permissions to the IIS console.This is more difficult to break.
The recommendations to remedy this issue in the following MSDN blog http://blogs.msdn.com/jongallant/archive/2007/11/25/linq-and-web-application-connection-strings.aspx is slightly wrong. He suggests that you remove the default constructor in the designer. This is bad because you would have to fix up the file every time you regenerate your dbml file. Instead, you should use what is provided to you and set the property on the designer for "Application Settings" to false and also do the following step every time you will go for the drag and drop your stored procedure and user defined functions.
- Right click on any white space in dbml file.
- Click on properties option.
- Please select drop down for Connection option in Properties window.
- Choose last option from drop down for Connection option which should be none .
- Close Properties window and press Save
Then allows you to define a default constructor in your own partial class that extends your dbml context designer classes like example:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration; // For Reading Connection String from Web.Config
///
/// Name: Constructor
/// Attribute: public
/// Type: class
/// Created By: Rajesh Prajapati
/// Creation Date: 16th Feb 2011
///
/// Modified By Modification Date Modification Reason
///
///
///
/// The class for handling Common Connection String from Web.Config file by the Default Constructor for all DBML Files
///
///
namespace DMNxtGen.DataAccess
{
///
/// Name: UserClassesDataContext
/// Attribute: public partial
/// Type: class
/// Created By: Rajesh Prajapati
/// Creation Date: 16th Feb 2011
///
/// Modified By Modification Date Modification Reason
///
///
///
/// The class for handling Common Connection String from Web.Config file by the UserClassesDataContext Constructor.
///
///
public partial class UserClassesDataContext
{
public UserClassesDataContext()
: base(ConfigurationManager.ConnectionStrings["WebConnectionString"].ConnectionString, mappingSource)
{
OnCreated();
}
}
}
In above example UserClassesDataContext is the class name of the LINQ to SQL(DBML) file and "WebConnectionString" is the name of the Connection String in Web.Config.
Happy Coding...............
Thanks,
Rajesh