none
is it possible to get a connection string to ado.net from a string connection for EF in the app config? RRS feed

  • Question

  • I have a connection string in my app config that is:

        <connectionStrings>
            <add name="MyEFConnection" connectionString="metadata=res://*/Model.AAA.csdl|res://*/Model.AAA.ssdl|res://*/Model.AAA.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=MyComputer\SQLEXPRESS;initial catalog=ABCD;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
          </connectionStrings>



    But in a particular case I want to use ADO.NET to execute a T-SQL, so I using this code:

         SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyEFConnection"].ConnectionString);



    But I get an exception that says that the Metada key word is not valid. There is any way to use the information in the connetion string for EF or I need to add a new string connection in my app config to the ADO.NET connection?



    Thanks.
    Wednesday, November 13, 2013 10:22 AM

Answers

  • No, the full connection string you need for the SqlConnection is in the ProviderConnectionString property of the EntityConnectionStringBuilder Class, with the metadata removed.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Thursday, November 14, 2013 12:38 PM

All replies

  • You can parse the entity connection string using the EntityConnectionStringBuilder Class in order to get only the info that is relevant to the SqlConnection Class:

    http://stackoverflow.com/questions/11840896/provider-connection-string-from-entity-framework


    Paul ~~~~ Microsoft MVP (Visual Basic)

    • Proposed as answer by Fred BaoModerator Thursday, November 14, 2013 7:14 AM
    • Marked as answer by ComptonAlvaro Thursday, November 14, 2013 8:48 AM
    • Unmarked as answer by ComptonAlvaro Thursday, November 14, 2013 9:06 AM
    • Marked as answer by ComptonAlvaro Thursday, November 14, 2013 9:32 AM
    • Unmarked as answer by ComptonAlvaro Thursday, November 14, 2013 9:33 AM
    Wednesday, November 13, 2013 3:02 PM
  • Hi

    SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyEFConnection"].ConnectionString);
    This will get all the connectionstring including
    "metadata=res://*/Model.AAA.csdl|res://*/Model.AAA.ssdl|res://*/Model.AAA.msl"
    which ADO.NET does not need actually.

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, November 14, 2013 7:17 AM
    Moderator
  • @Paul: in the link that you give me the problem is that the user wants to get the provider and there are a property to get it. But I need data source and initial catalog that are in the connection string.

    How can I get this information? I need to parse the string?


    Thursday, November 14, 2013 9:10 AM
  • No, the full connection string you need for the SqlConnection is in the ProviderConnectionString property of the EntityConnectionStringBuilder Class, with the metadata removed.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Thursday, November 14, 2013 12:38 PM