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

Tagged as

Set the connection string for Entity Framework at runtime

, 2 Aug 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
This shows how to set or change the connection string for Entity Framework at run time (programmatically)
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)

Share

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.

Comments and Discussions

 
Generalworks (entity framework 6.1, .Net 4.5.1) PinmemberMattens15-Oct-14 12:03 
GeneralMy vote of 1 PinmemberMember 1105237030-Sep-14 0:32 
SuggestionSuggestion for NetFramework 4.5 Pinmemberrafaelvareto18-Aug-14 23:45 
GeneralMy vote of 2 Pinmembermeloukoud23-Jun-14 2:04 
GeneralMy vote of 1 PinmemberMember 1085838214-Jun-14 1:02 
QuestionExcellent!! PinmemberMember 42609687-Apr-14 13:27 
GeneralThanks. PinmemberCIAP10-Jun-13 10:48 
QuestionFor Entity Framework Pinmemberaomidi5-Feb-13 0:53 
Is a good way
But this does not work in version 4.5
what should I do?
GeneralMy vote of 2 PinmemberMichael Freidgeim15-Nov-12 8:03 
QuestionIs esb required? PinmemberMichael Freidgeim15-Nov-12 7:49 
GeneralMy vote of 5 Pinmembereibbed7-Sep-12 11:07 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.141022.2 | Last Updated 2 Aug 2011
Article Copyright 2011 by Bahram Ettehadieh
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid