Click here to Skip to main content
Click here to Skip to main content

Set the connection string for Entity Framework at runtime

By , 2 Aug 2011
 
string connectionString = new System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
 
System.Data.SqlClient.SqlConnectionStringBuilder scsb = new System.Data.SqlClient.SqlConnectionStringBuilder(connectionString);
 
EntityConnectionStringBuilder ecb = new EntityConnectionStringBuilder();
ecb.Metadata = "res://*/Sample.csdl|res://*/Sample.ssdl|res://*/Sample.msl";
ecb.Provider = "System.Data.SqlClient";
ecb.ProviderConnectionString = scsb.ConnectionString;
 
dataContext = new SampleEntities(ecb.ConnectionString);
This also would be useful if you want to encrypt the connection string using your own methods.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Bahram Ettehadieh
Web Developer
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
I hold a BS degree in software engineering and am a Microsoft Certified Solution Developer(MCSD).
I have more than 8 years of experience in .NET developement, mostly web develop using C# and ASP.NET.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralThanks.memberCIAP10-Jun-13 10:48 
QuestionFor Entity Frameworkmemberaomidi5-Feb-13 0:53 
GeneralMy vote of 2memberMichael Freidgeim15-Nov-12 8:03 
QuestionIs esb required?memberMichael Freidgeim15-Nov-12 7:49 
GeneralMy vote of 5membereibbed7-Sep-12 11:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130619.1 | Last Updated 2 Aug 2011
Article Copyright 2011 by Bahram Ettehadieh
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid