locked
extract connection string from DbContext RRS feed

  • Question

  • ...using .net 4 and System.Data.Entity.dll v4.0.30319

    I'm using EF for 90% of my db stuff, but need to use a stored proc as EF just produces some ugly sql and think its silly to store the same connection string twice in the config.

    How can I extract the connection string from the DbContext that I can use with the  SqlConnection class?


    Thank Kevin

    Thursday, January 31, 2013 3:57 PM

Answers

  • Hi kevcoder;

    One way to get the SQL connections string from the config file is as follows. 

    // Add a reference to the System.Configuration.dll to the project.
    
    using System.Configuration;
    
    // In the following statement replace NorthwindEntities with the name of the connectionSteing in the configuration file
    var efConnStr = ConfigurationManager.ConnectionStrings["NorthwindEntities"].ConnectionString;
    var sqlConnStr = new EntityConnectionStringBuilder(efConnStr).ProviderConnectionString;
    
    // The variable sqlConnStr is the SQL connection string.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, January 31, 2013 8:00 PM