Skip to content

Should I be able to use the EF Core tools to apply migrations when using an interceptor to set my connection string #31055

@mdemler

Description

@mdemler

Should the EF Core tools work when using an interceptor to set the connection string?

We're using EF Core 7 with PosgreSQL. We're deriving from DbConnectionInterceptor and overriding ConnectionOpenAsync to set the connection string using a value that we construct at runtime.

Previously, before switching to this model, we were able to use dotnet ef database update to apply generated migrations to the database, but now we're receiving a "Host can't be null" error.

This error does look to originate from with Npgsql, but before bugging those folks, I wanted to check to see if this is something that should even be supported. I wasn't able to find details on how the tools go about establishing a database connection. Both our current and prior approaches were implemented by overriding OnModelCreating() for the DbContext.

Here's our current approach:

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            var interceptor = new DbConnectionInterceptorDerivative();

            optionsBuilder.AddInterceptors(interceptor);

            optionsBuilder.UseNpgsql();
        }

Here's the callstack of the error reported by the tool:

System.ArgumentException: Host can't be null
   at Npgsql.NpgsqlConnectionStringBuilder.PostProcessAndValidate()
   at Npgsql.NpgsqlConnection.SetupDataSource()
   at Npgsql.NpgsqlConnection.set_ConnectionString(String value)
   at Npgsql.NpgsqlConnection..ctor(String connectionString)
   at Npgsql.NpgsqlConnection.CloneWith(String connectionString)
   at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlRelationalConnection.CloneWith(String connectionString)
   at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlDatabaseCreator.Exists(Boolean async, CancellationToken cancellationToken)
   at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlDatabaseCreator.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String connectionString, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Host can't be null

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions