Skip to main content

SqlConnection from ObjectContext.Connection

Following is C# code to get the SqlConnection from ObjectContext.Connection.
This is usefully to run dynamically created query using ADO.NET using connection string specified into the EDMX ObjectContext object.

C# Code Example :

using (EntityContext context = new EntityContext())
{
EntityConnection ec = (EntityConnection)context.Connection;
SqlConnection sc = (SqlConnection)ec.StoreConnection;

SqlDataAdapter da = new SqlDataAdapter(strQuery, sc);
DataSet ds = new DataSet();
da.Fill(ds);

return ds.Tables[0];

}


Comments