August 07, 2012

Change Entity Framework ConnectionString‎

Problem: 
- Dynamically change Entity Framework Connection String‎
- How to change the connection string in the EntityFramework ...
- How should I edit an Entity Framework connection string ...
- change db name in connection string at runtime in Entity Framework
- Cannot change the connectionstring with Entity Framework 4.1 code ...
- Entity Framework - how can I change connection string to be relative?
- Entity Framework Connection String Trouble - Stack Overflow

Solution:
Just add System.Configuration.dll to Refrences of your Project and then use of this code
ConfigurationManager.ConnectionStrings[0].ConnectionString = blah blah blah
ConfigurationManager.ConnectionStrings["XYZEntities"].ConnectionString = blah blah blah

Error in Solution:
Error: 
The configuration is read only.
Correction: 

var Configuration = ConfigurationManager.ConnectionStrings["amaBoxOfficeEntities"];
typeof(ConfigurationElement).GetField("_bReadOnly", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(Configuration, false);
Configuration.ConnectionString = blah blah blah


2 comments:

Sergio said...

perfect, that is exactly what i was looking for... thank you

Sergio said...

perfect, that is exactly what i was looking for... thank you