C# & .NET

How to get the EF Core Connection String?

Reading Time: < 1 minute.

A while ago, I had a situation where a DbContext was misbehaving after deployed to an Azure App Service, and I needed to check the connection string it’s using directly in the code.

However, finding the right method actually took me googling, as there were plenty of examples for Entity Framework for .NET Framework, but next no nothing for Entity Framework Core. Let’s fix that.

Long story short, here’s how:

 
ApplicationDbContext db; // injected in the constructor of the controller
 
db.Database.GetDbConnection().ConnectionString // fetch the DbConnection object and grab its Connection String

This applies to .NET Core + EF Core (at least starting form around version 3.0).

Oh – a quick word of advice: The connection string will contain your password. Don’t log it anywhere as is. If you plan to push it to Application Insights or something, remove the password from the string before doing that.

mm
0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments