Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework getting an sql connection

In the light of Closing connections explicitly in Entity Framework and http://msdn.microsoft.com/en-us/library/bb738582%28v=vs.90%29.aspx it seems that I should be using the context to create connections rather than doing the following

using (SqlConnection con = new SqlConnection("Persist Security Info=False;Integrated Security=true;Initial Catalog=Remember;server=(local)"))
{
    ...
}

My understanding is that I'll

  • Get rid of the connection string
  • Utilize connection pooling built into EF

But how do I acquire an SQL connection through the context?

like image 472
Carlo V. Dango Avatar asked Mar 12 '11 22:03

Carlo V. Dango


People also ask

How do I get connection string from EDMX?

You can use DbContext with an edmx file, and you will still have an EntityConnection string rather than a Database ConnectionString when using this method.


1 Answers

I believe the easiest way to create a SqlConnection in EF6 is to do the following:

DBContext context = new DBContext();
SqlConnection sqlconn = new SqlConnection(context.Database.Connection.ConnectionString);
like image 145
Varun Mathur Avatar answered Oct 11 '22 22:10

Varun Mathur