Create your DbConnection
manually and pass it to the DbContext
constructor as follows:
var conn = DbProviderFactories.GetFactory("MY_CONN_PROVIDER").CreateConnection();
conn.ConnectionString = "MY_CONN_STR";
new DbContext(conn, true);
Notice the second parameter bool contextOwnsConnection
is true
. Since you don’t re-use the connection elsewhere, it lets the context manage the connection and Dispose()
it when needed.