Click here to Skip to main content
Sign Up to vote bad
good
See more: C#4.0entityApp.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 - 7:25

Comments
ryanb31 - 17 Oct '12 - 13:46
You want to change a config file? I'm confused, all you do is change the file. What's the question?
Marcus Kramer - 17 Oct '12 - 14:42
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[^]
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 464
1 Mahesh Bailwal 373
2 Maciej Los 215
3 CPallini 175
4 Rohan Leuva 175
0 Sergey Alexandrovich Kryukov 9,402
1 OriginalGriff 7,204
2 CPallini 3,933
3 Rohan Leuva 3,211
4 Maciej Los 2,743


Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 17 Oct 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid