Álvaro García
Álvaro García

Reputation: 19396

is it possible to get the connection string to ADO from a connection string for entity framework?

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 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.

Upvotes: 0

Views: 109

Answers (1)

hquinn
hquinn

Reputation: 100

First you need to replace the & quot; with a single quot. Then create an entity EntityConnection from your connection string:

EntityConnection entityConnection =  new EntityConnection(connectionString);

And then you can get the connection string to ADO from the EntityConnection:

 string ADOConnectionString = entityConnection.StoreConnection.ConnectionString;

Upvotes: 1

Related Questions