Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
how to change connection string entity framework in runtime

code in my appconfig file :


XML
<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="RelationAtOfficeEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.;Initial Catalog=RelationAtOffice;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient"/>
  </connectionStrings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>



I want to change the following code :

XML
    <?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="RelationAtOfficeEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="Data Source=127.10.10.23,1356;Initial Catalog=RelationAtOffice;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient"/>
  </connectionStrings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>



just data source changed.

How is it done?
Posted
Comments
ZurdoDev 17-Oct-12 13:46pm    
You want to change a config file? I'm confused, all you do is change the file. What's the question?
fjdiewornncalwe 17-Oct-12 14:42pm    
Why would you need to change the connection string at runtime. I completely understand if you want to change the connection string on a deployment, but at runtime doesn't really make sense. Could you explain why, please?

1 solution

You can create objectcontext based on dynamic connection string if you use the non-default overloaded constructor. Keep the format of connection string in appsettings and change the datasource dynamically and create your ObjectContext, till the time schema remains same it will work perfectly.


Please refer documentation at. http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.objectcontext.aspx[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900