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

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

Leave a Comment