Set LINQ DSN for DataContext?

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).

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.

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 Nov 5 '08 at 7:50 Is there no way to initialise the DataContext with a custom DSN on Application startup? – Christoph Nov 5 '08 at 7:50 You don't want a single DataContext throughout the lifespan of your app. Create them as you need them.

– Keltex Nov 6 '08 at 2:58.

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.

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 Nov 6 '08 at 11:01.

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 cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions