Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Core: How to get the Connection from the DbContext?

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.

like image 314
nico9T Avatar asked Jan 30 '17 12:01

nico9T


People also ask

How do I get the EF core connection string?

The providerName setting is not required on EF Core connection strings stored in App. config because the database provider is configured via code. You can then read the connection string using the ConfigurationManager API in your context's OnConfiguring method. You may need to add a reference to the System.

How does DbContext work in Entity Framework?

As per Microsoft “A DbContext instance represents a session with the database and can be used to query and save instances of your entities. DbContext is a combination of the Unit Of Work and Repository patterns.” In simplified way we can say that DbContext is the bridge between Entity Framework and Database.

What is DbContext in EF core?

DbContext is a combination of the Unit Of Work and Repository patterns. DbContext in EF Core allows us to perform following tasks: Manage database connection. Configure model & relationship. Querying database.


2 Answers

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

like image 195
Developer Avatar answered Oct 02 '22 06:10

Developer


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.

like image 29
Wael Galal El Deen Avatar answered Oct 02 '22 05:10

Wael Galal El Deen