83

I am trying the new Entity Framework Core with MySQL Connector.

I can get a valid DbContext and write into the database so everything has been setup correctly.

I need to get the Connection from the DbContext because I have to test for it at application starting using a connection.Open() inside a try statement. If there is not a valid connection, the console app should try to start MySQL Server and retry.

How can I get the Connection from the DbContext?

Before EF6 by context.Connection. After EF6 by context.Database.Connection.

It seems the latest has been removed too from EFCore.

9
  • 2
    there is something like - dbContext.Database.OpenConnection() or dbContext.Database.GetDbConnection() in EF Core - would that be useful? Commented Jan 30, 2017 at 12:32
  • No, unfortunately not. I can't see anything like context.Database.OpenConnection() or context.Database.GetConnection() method, otherwise I would have simply called them. Commented Jan 30, 2017 at 12:44
  • 1
    Dependencies: "Microsoft.EntityFrameworkCore": "1.0.0", "MySql.Data.Core": "7.0.4-IR-191", "MySql.Data.EntityFrameworkCore": "7.0.4-IR-191". Commented Jan 30, 2017 at 12:58
  • 3
    Try installing this package - nuget.org/packages/Microsoft.EntityFrameworkCore.Relational Commented Jan 30, 2017 at 12:59
  • 1
    Thanks. This fixed it! Commented Jan 30, 2017 at 13:08

2 Answers 2

117

The Microsoft.EntityFrameworkCore.Relational (https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Relational/) package provides extension methods for this - you can use dbContext.Database.OpenConnection() or dbContext.Database.GetDbConnection() to get the DbConnection object.

Note: if you have Microsoft.EntityFrameworkCore.SqlServer installed then you don't have to explicitly install this package

Sign up to request clarification or add additional context in comments.

7 Comments

GetDbConnection() in 2.0
Make sure to include the extensions, this was the problem for me. (using Microsoft.EntityFrameworkCore.Extensions;)
I struggled with finding this as well. Just add a using Microsoft.EntityFrameworkCore statement. As long as you have Microsoft.EntityFrameworkCore.SqlServer or Microsoft.EntityFrameworkCore.Relational included in your nuget deps, you should then see the extension methods.
GetDbConnection() fails when called second time on Database. I am using 'using (var connection = _dbContext.Database.GetDbConnection()) {}' block to invoke some commands. connection is Open and Closed within the using block, but yet when I run the same method for the second time I get faulty connection (ConnectionString missing, some property exceptions. What's causing it? Any workaround to this .NET Core EF bug?
@RadekStrugalski this might be relevant: github.com/aspnet/EntityFrameworkCore/issues/7810
|
18

You can do the following :

  1. If you are using System.Data.SqlClient change it to Microsoft.Data.SqlCLient since the later library will accept excplicit casting from the object of the DBContext in both EntityFramework and EntityFrameWorkCore.
  2. Create a command from the DBContext connection and Cast it to your SQLCommand by
using (SqlCommand cmd = (SqlCommand)database.GetDbConnection().CreateCommand())
  1. Check the connection state and open it if necessary.
if (cmd.Connection.State != ConnectionState.Open)
{
    cmd.Connection.Open();
}
  1. If there is a transaction in the DBContext add it to the command.
if (database.CurrentTransaction != null)
{
    cmd.Transaction = database.CurrentTransaction.GetDbTransaction();
}

By doing this you will avoid any problems can appear when using comands outside the context.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.