This post was most recently updated on February 26th, 2021.
2 min read.This article describes how to access and extract the connection strings from your Entity Framework (Core) database context objects. This is quite convenient if you need to display or log the connection string used for your current DbContext for some reason ā or if you somehow form your DbContext objects dynamically, and need to verify which connection string youāre using.
Iām sure there are other use cases, too. You probably have an interesting one, if you landed on this page!
Problem
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. I had reason to think my code was grabbing an outdated connection string and using a wrong database ā and as you can probably imagine, that could cause some issues!
However, finding the right method actually took me googling, as there were plenty of examples for Entity Framework for .NET Framework, but next to nothing for Entity Framework Core.
So ā letās fix that!
Long story short, hereās how:
Solution
Time needed:Ā 10 minutes
How to extract the connectionstring from your Database Context in C#?
- Include the following using statements
These work for Microsoft SQL Server connection strings ā you might need something else if your data source is different.
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore; - Instantiate your DbContext (or inject it)
Whatever you do, just have it set ā this is the variable weāll be using later on as well:
// Injected in the constructor of the controller
// or instantiated in the code
ApplicationDbContext db; - Get the connection and access ConnectionString property
Like shown below:
// Fetch the DbConnection object and
// grab its Connection String
db.Database.GetDbConnection().ConnectionString
Oh ā a quick word of advice:
The connection string might contain your password. As Zaki Bhai points out in the comments below, this happens only if Persist Security Info = True has been specified in the connection string.
Most of the time, you donāt want to do this ā and definitely do not log the connection string anywhere as is. If you plan to push it to Application Insights or something, remove the password from the string before doing that. Below Iāll show one example on how to do that! - (Optional) Sanitize your connection string to remove the password š
In case youāre displaying or logging this information somewhere, you need to (at the very least) remove or replace the password.
This sample would be one way to achieve just that:Regex r = new Regex("Password='.+'");
string dummyPassword = "Password='Password123'";
string cs = db.Database.GetDbConnection().ConnectionString;
string csSanitized= r.Replace(cs, dummyPassword);
_monitor.TrackEventDebug("ReadOnlyDbContext", "ConnectionString", csSanitized);
Now your connection string will be logged with a (hopefully) obvious dummy password. :)
And that should be it!
This instruction applies to .NET Core + EF Core (at least starting form around version 3.0).
- How to close the Sidebar in Microsoft Edge - December 17, 2024
- How to stop Surface Headphones from autoplaying audio when you place them on the desk? - December 10, 2024
- āPhone Linkā permanently disabled and Windows claiming āSome of these settings are managed by your organizationā? (Probably) an easy fix! - December 3, 2024