locked
Get connectionString from xml file and setting it into App.config RRS feed

  • Question

  • Dear all,

    I need your help to solve my issue.

    I developed a wpf application that uses Entity Framework 4.1 to manage db layer.

    I need this requirement. When I start application I'd like that connection string is read from an xml file of application.

    Actually I've in App.config

     

    <connectionStrings><add name="MyWebDBEntities" connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SQLite;provider connection string=&quot;data source=C:\Users\user\Desktop\dbSqliteMyWeb.db3&quot;" providerName="System.Data.EntityClient" /></connectionStrings>
    


    I'd like to get connectionString property, that now is hardcoded in the file App.config, from a xml file inside the project.


    Is it possible make this property dynamic? Could you please let me know with a simple code how getting connectionString from a xml file?

     

    Thanks ,
    Regards

    Monday, June 27, 2011 1:08 PM

Answers

  • Hi Danibat;

    The ObjectContext has a default constructor and when used it will get the connection string information from the App.config file. It also has three overloads, ObjectContext constructors, one of which takes a single string with the connection string information.

    // Connection String information parsed from the XML file and should look like this
    // replace the two &quot; with two double quote marks, "" . The following
    // connection string is from your question
    string conStr = @"metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SQLite;provider connection string=""data source=C:\Users\user\Desktop\dbSqliteMyWeb.db3""";
    
    // The new ObjectContext will be created with the above connection string
    ObjectContext ctx = new ObjectContext(conStr);
    
    

     


    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Jackie-Sun Thursday, July 7, 2011 9:44 AM
    Monday, June 27, 2011 3:46 PM

All replies

  • On 6/27/2011 9:08 AM, Danibat wrote:
    > Dear all,
    >
    > I need your help to solve my issue.
    >
    > I developed a wpf application that uses Entity Framework 4.1 to manage
    > db layer.
    >
    > I need this requirement. When I start application I'd like that
    > connection string is read from an xml file of application.
    >
    > Actually I've in App.config
    >
    > <connectionStrings><add  name="MyWebDBEntities"  connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SQLite;provider connection string=&quot;data source=C:\Users\user\Desktop\dbSqliteMyWeb.db3&quot;"  providerName="System.Data.EntityClient"  /></connectionStrings>
    >
    >
    > I'd like to get connectionString property, that now is hardcoded in the
    > file App.config, from a xml file inside the project.
    >
    >
    > Is it possible make this property dynamic? Could you please let me know
    > with a simple code how getting connectionString from a xml file?
     
    I don't think you can do it. I think EF is going to look for the
    connection string in the root.config for the program/application.
     
     
    Monday, June 27, 2011 1:31 PM
  • string _connectionString =ConfigurationManager.ConnectionStrings["MyWebDBEntities"].ConnectionString;

     

    the above code would retrieve the connection string for you if only its in app.config file. Pass this as the parameter to the DBContext constructor each time you initialize it.


    Pratap --Mark the best replies as answers!
    Monday, June 27, 2011 2:38 PM
  • Hi Danibat;

    The ObjectContext has a default constructor and when used it will get the connection string information from the App.config file. It also has three overloads, ObjectContext constructors, one of which takes a single string with the connection string information.

    // Connection String information parsed from the XML file and should look like this
    // replace the two &quot; with two double quote marks, "" . The following
    // connection string is from your question
    string conStr = @"metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SQLite;provider connection string=""data source=C:\Users\user\Desktop\dbSqliteMyWeb.db3""";
    
    // The new ObjectContext will be created with the above connection string
    ObjectContext ctx = new ObjectContext(conStr);
    
    

     


    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by Jackie-Sun Thursday, July 7, 2011 9:44 AM
    Monday, June 27, 2011 3:46 PM