Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 entity App.config , +
how to change connection string entity framework in runtime
 
code in my appconfig file :
 

<?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 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 17-Oct-12 8:25am
Comments
ryanb31 at 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?
Marcus Kramer at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  

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

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 17 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100