views:

808

answers:

3

Hi,

I normally store all my configs in the registry. Even though I have started using LINQ I would not like to have the DSN in the web.config, but rather let it stay in the registry and attach it (maybe in the Application Start Event) to the System Config.

How can this be done?

Thanx for any ideas!

edit: to make it clear: I do not want to write to the web.config file, I just want to keep the dsn (encrypted) anywhere else than the web.config, so I have the same web.config on all development stages (local, staginf, live, backup).

Christoph

Solution Code in VB.Net:

1) Add a new Class, with one method, which inherits from the original Datacontext:

Public Class MyDatabaseDataContext

Inherits DatabaseDataContext

Public Sub New()
    MyBase.New(Settings.DSN)
End Sub

 End Class

2) Use this Class in all Linq Datasources instead of the original Context.

 ContextTypeName="MyProject.MyDatabaseDataContext
+1  A: 

Why not just keep it encrypted in the web.config? It's fairly easy to encrypt just the connection strings in the web.config on Application_Start, if you don't want to use aspreg_iis. You can even edit the encrypted web.config afterwards using the IIS Admin tool.

Configuration config = WebConfigurationManager.OpenWebConfiguration(
                               HttpContext.Current.Request.ApplicationPath );
ConfigurationSection section = config.Sections["connectionStrings"];
if (!section.SectionInformation.IsProtected
    && !GlobalConfiguration.ApplicationVersion.EndsWith( "dev" )) // don't encrypt dev
{
    section.SectionInformation.ProtectSection( "DataProtectionConfigurationProvider" );
    config.Save();
}

If you must store it in the registry. You can always use the constructor on the DataContext that takes a connection string and pass in the value you read from the registry. Presumably you'd store this in the Application store so you only have to read from the registry once.

Scott Guthrie has a good references page for encrypting your web config, though most of the examples use aspreg_iis. I prefer to do it on Application_Start so I don't forget to encrypt it on accident.

tvanfosson
I already used the overwriting the constructor of the DataContext, but this has 2 issues: I have to delete the default constructor in the DB.designer.vb and any time I change the dbml, the constructor in the DB.designer.vb is re-created.
Christoph
Is there no way to initialise the DataContext with a custom DSN on Application startup?
Christoph
You don't want a single DataContext throughout the lifespan of your app. Create them as you need them.
Keltex
+1  A: 

Here's what I do. I have a a base class for my DataContext. It's called DataContextBase and is generated by sqlmetal.exe. I have a derived class called DataContext which is what is used in my Linq calls. It looks like this:

public class DataContext : DataContextBase
{
    public DataContext()
        : base(ConnectionHolder.ConnectionString)
    {
    }
}

I have a static class in my library called ConnectionHolder which stores the connection string:

public static class ConnectionHolder
{
    static string _ConnectionString;

    public static string ConnectionString
    {
        get { return _ConnectionString; }
        set { _ConnectionString = value; }
    }
}

(note: this is separate from DataContext because there are places in my app outside of Linq that I use the connection string). At app startup I say ConnectionHolder.ConnectionString = (wherever you store the connection string).

Keltex
A: 

This is a fairly reasonable requirement when working with legacy code.

The DataContext class is a partial class, so I just add a static factory method that loads the configuration setting and creates the datacontext result.

Frank Schwieterman
Which method do you add? In the DB.designer.vb a public sub new already esists, so if a add an public sub new in my DB.vb I get an duplicate function definition error. If I delete this method in the DB.designer.vb, everything workes fine, but if a edit the dbml the sub everytime is recreated...
Christoph