Overriding ConnectionString in DataContext

Today at work, one of my co-workers mentioned an issue that he was loosing ConnectionString every time he would modify the linq to SQL model in the dbml file. What he was essentially doing was getting rid of the default constructor and adding a partial class where in the default constructor based on business rules, he would fetch the right ConnectionString either from web.config, app.config or some other xml file. Obviously the problem here was, you cant have two default constructors. Because every time you would modify the dmbl file any changes you have made to the designer.cs would get lost. But a simpler solution to this would simply be creating your own partial DataContext class and adding the OnCreated partial method which gets called in the default constructor. Here is a simply implementation where I am reading the ConnectionString from web.config ConnectionString section.

image

No Comments