When applying migration --connection parameter is not respected in 5.0.6
See original GitHub issueI’ve upgraded from version 5.0.2 to 5.0.6 and it seems that --connection
parameter is not respected everywhere.
Command I use in my CI pipeline (worked in 5.0.2):
dotnet ef database update -p {myDataContextProjectPath} -s {myStartupProject} -v --connection "Host=$POSTGRES_HOST;Port=$POSTGRES_PORT;Database=$POSTGRES_DB;Username=$POSTGRES_USER;Password=$POSTGRES_PASSWORD"
.
Where $POSTGRES_HOST
is postgres
and $POSTGRES_PORT
port is 5432
.
I turned on verbose logging and I think I’ve found why this happens:
5.0.2 log:
Using DbContext factory ‘MyContextFactory’. Using context ‘MyDataContext’. Finding design-time services for provider ‘Npgsql.EntityFrameworkCore.PostgreSQL’… Using design-time services from provider ‘Npgsql.EntityFrameworkCore.PostgreSQL’. Finding design-time services referenced by assembly ‘MyStartupProject’… Finding design-time services referenced by assembly ‘MyDataContextProject’… No referenced design-time services were found. Finding IDesignTimeServices implementations in assembly ‘MyStartupProject’… No design-time services were found. Migrating using database ‘MyDb’ on server ‘tcp://postgres:5432’. Creating DbCommand for ‘ExecuteScalar’. Created DbCommand for ‘ExecuteScalar’ (11ms). Opening connection to database ‘MyDb’ on server ‘tcp://postgres:5432’. Opened connection to database ‘MyDb’ on server ‘tcp://postgres:5432’.
5.0.6 log:
Using DbContext factory ‘MyContextFactory’. Using context ‘MyDataContext’. Finding design-time services for provider ‘Npgsql.EntityFrameworkCore.PostgreSQL’… Using design-time services from provider ‘Npgsql.EntityFrameworkCore.PostgreSQL’. Finding design-time services referenced by assembly ‘MyDataContextProject’… Finding design-time services referenced by assembly ‘MyStartupProject’… No referenced design-time services were found. Finding IDesignTimeServices implementations in assembly ‘MyStartupProject’… No design-time services were found. Migrating using database ‘MyDb’ on server ‘tcp://postgres:5432’. Opening connection to database ‘MyDb’ on server ‘tcp://localhost:5252’. An error occurred using the connection to database ‘MyDb’ on server ‘tcp://localhost:5252’.
It seems that the values are now taken from appsettings.json
- host with value localhost
and port with value 5252
are defined in my appsettings.Development.json
.
My data context factory did not change. Data factory context is located in ‘MyDataContextProject’ along with MyDataContext
. Code:
public class MyContextFactory: IDesignTimeDbContextFactory<MyDataContext>
{
private readonly IConfiguration _configuration;
public DataContextFactory()
{
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{environment}.json", true);
_configuration = config.Build();
}
public MyDataContext CreateDbContext(string[] args)
{
var connectionString = _configuration["ConnectionStrings:DefaultConnection"];
var options = new DbContextOptionsBuilder<MyDataContext>()
.UseNpgsql(connectionString)
.Options;
return new MyDataContext(options);
}
}
Other useful information: I am using ASP.NET 5, EF Core 5.0.6, Npgsql 5.0.2 (implicitly upgraded to 5.0.5 when I upgraded this package to 5.0.6) and postgres 11.
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (5 by maintainers)
I don’t remember my previous investigation - I just tried to re-repro this, and could not.
I’m using the following code;
Note that the password in UseNpgsql is intentionally wrong.
When executing
dotnet ef database update
, this expectedly fails because of the password. However, when executingdotnet ef database update --connection "Host=localhost;Username=test;Password=test"
this succeeds, indicating that the hard-coded connection string is overridden by the command-line parameter, as it should. I verified this on EF Core 5.0.6 and 6.0.1.@avidenic can you take another look at this and confirm if you’re still running into it? If so, can you tweak my above attempted repro to make it show the bad behavior?
Closing for lack of response.