This is a brief translation of the question How to get ConnectionString from EF7 DbContext

I use EF core 2.0 for typical CRUD operations and at the same time Dapper for more complex and fast requests.

In the startup.cs file, I inject DbContext in the DAL layer repository for EF, and dapper uses a connection string for work. I want to make DbContext EF get the string to insert into the Dapper request.

How can I get a connection string from DbContext?

  • Interestingly, is there any difference so that in startup.cs you can immediately forward Configuration.GetConnectionString("MyConnectionname") to the constructor? - AK

1 answer 1

In EF.Core you can use:

 var conn = context.Database.GetDbConnection(); ConnectionString = conn?.ConnectionString; conn?.Dispose();