Skip to content

Instantly share code, notes, and snippets.

@tekguy
Created October 23, 2014 15:19
Show Gist options
  • Save tekguy/dd717bf84639940f8f02 to your computer and use it in GitHub Desktop.
Save tekguy/dd717bf84639940f8f02 to your computer and use it in GitHub Desktop.
Get a SqlConnection from a DbContext
private SqlConnection GetSqlConnection(DbContext dbContext)
{
var ec = dbContext.Database.Connection;
var adoConnStr = ec.ConnectionString;
return new SqlConnection(adoConnStr);
}
@adrianneg
Copy link

OR Get a SqlConnection from a DbContext

public class TBill_DbConnect : DbContext
{
SqlConnection conn;

    public TBill_DbConnect()
        : base("name=dbConnection")
    {
        try
        {
            conn = this.Database.Connection as SqlConnection; 
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
    }

@amaliraza
Copy link

And In .Net 5?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment