Contents
data/authors/Paul Logan.json

Why is my Development environment ConnectionString being ignored when debugging

TL;DR

The ConnectionString defined in any ASP.NET Core appsetting JSON file will be overridden by the connection string that was created by the scaffolder in the OnConfiguring method of the scaffolded DbContext class.

The solution is to delete the whole OnConfiguring function from the scaffolded DbContext class file.

.Net Core Environment Settings

The environments we are most familiar with are development (dev) and production (live). They allow settings to be automatically inherited and/or overriden depenedent on the environment your .Net Core application is executing under.

Inherited from where? From the default configuration file, which is the appsettings.json file highlighted below.

When debugging, VS Code should use the appsettings.Development.json file to overwrite any settings that have the same key in the default appsettings.json file.

The default and dev appsettings files
The default and dev appsettings files

Previous technologies I have used to achieve this same result in .Net Framework projects were:

OnConfiguring UseSqlServer trumps all

This situation arises when you use the .Net CLI tools to reverse engineer an existing database and create entity type classes based on the database schema.

The first argument required for the “dotnet ef dbcontext scaffold” command is the connection string to the database. This is then passed into the OnConfiguring method in the scaffolded code, and used by the UseSqlServer method to connect to the database server.

Progress

There has been progress made in this area, with Microsoft adding a new, much requested, switch to suppress the default generation of the OnConfiguration method containing the connection string, appropriately named –no-onconfiguring.

https://github.com/dotnet/efcore/issues/10432 https://github.com/dotnet/efcore/pull/20811