Click here to Skip to main content
14,143,311 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


11 bookmarked
Posted 2 Aug 2011
Licenced CPOL

Set the connection string for Entity Framework at runtime

, 2 Aug 2011
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.


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.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
Cahit SEÇMEN25-Apr-16 9:46
memberCahit SEÇMEN25-Apr-16 9:46 
GeneralMy vote of 5 Pin
Valdir Martins17-Jan-16 7:19
memberValdir Martins17-Jan-16 7:19 
QuestionNo App.Config Pin
stixoffire31-Jul-15 3:37
memberstixoffire31-Jul-15 3:37 
GeneralMy vote of 1 Pin
Cem Usta28-Jan-15 4:47
memberCem Usta28-Jan-15 4:47 
Generalworks (entity framework 6.1, .Net 4.5.1) Pin
Mattens15-Oct-14 12:03
memberMattens15-Oct-14 12:03 
GeneralMy vote of 1 Pin
Member 1105237030-Sep-14 0:32
memberMember 1105237030-Sep-14 0:32 
SuggestionSuggestion for NetFramework 4.5 Pin
rafaelvareto18-Aug-14 23:45
memberrafaelvareto18-Aug-14 23:45 
GeneralRe: Suggestion for NetFramework 4.5 Pin
User 1066841010-Dec-15 16:47
memberUser 1066841010-Dec-15 16:47 
GeneralMy vote of 2 Pin
meloukoud23-Jun-14 2:04
membermeloukoud23-Jun-14 2:04 
GeneralMy vote of 1 Pin
Member 1085838214-Jun-14 1:02
memberMember 1085838214-Jun-14 1:02 
QuestionExcellent!! Pin
Member 42609687-Apr-14 13:27
memberMember 42609687-Apr-14 13:27 
GeneralThanks. Pin
CIAP10-Jun-13 10:48
memberCIAP10-Jun-13 10:48 
QuestionFor Entity Framework Pin
aomidi5-Feb-13 0:53
memberaomidi5-Feb-13 0:53 
GeneralMy vote of 2 Pin
Michael Freidgeim15-Nov-12 8:03
memberMichael Freidgeim15-Nov-12 8:03 
QuestionIs esb required? Pin
Michael Freidgeim15-Nov-12 7:49
memberMichael Freidgeim15-Nov-12 7:49 
GeneralMy vote of 5 Pin
eibbed7-Sep-12 11:07
membereibbed7-Sep-12 11:07 

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

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

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