question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

When applying migration --connection parameter is not respected in 5.0.6

See original GitHub issue

I’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:closed
  • Created 2 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
rojicommented, Jan 14, 2022

I don’t remember my previous investigation - I just tried to re-repro this, and could not.

I’m using the following code;

Console.WriteLine("Hello world");

public class MyDataContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }

    public MyDataContext(DbContextOptions options)
        : base(options)
    {
    }
}

public class Blog
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class MyContextFactory: IDesignTimeDbContextFactory<MyDataContext>
{
    public MyDataContext CreateDbContext(string[] args)
    {
        var options = new DbContextOptionsBuilder<MyDataContext>()
            .UseNpgsql(@"Host=localhost;Username=test;Password=bad_password")
            .Options;
        return new MyDataContext(options);
    }
}

Note that the password in UseNpgsql is intentionally wrong.

When executing dotnet ef database update, this expectedly fails because of the password. However, when executing dotnet 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?

0reactions
rojicommented, Jun 5, 2022

Closing for lack of response.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Pass CreateDbContext Args Parameters?
7 or newer you can pass custom 'application arguments' into the CreateDbContext method by appending -- to the end of the command, followed...
Read more >
EF Core tools reference (Package Manager Console)
Lists available migrations. Parameters: Parameter, Description. -Connection <String>, The connection string to the database. Defaults ...
Read more >
Release Notes for Flyway Engine
We have improved support for `WHENEVER SQLERROR EXIT` in Oracle SQL*Plus. This now works on its own and no longer needs to be...
Read more >
MySQL Connector/NET Release Notes
Changes in MySQL Connector/NET 7.0.1 (Not released, Internal) . ... An application using Entity Framework code-first migration without the default system ...
Read more >
5.0 Changelog — MongoDB Manual
SERVER-68039 Old pymongo version 3.10.1 on MongoDB v5.0 causes Invariant failure (message.operation() == dbMsg) after connection reset by peer.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found