Error updating database using dotnet ef database update when using postgresql
- Dominant language
- C#
- Stars
- 6.3k
- Forks
- 991
- Avg merge
- 2d 15h
- Merged PRs (30d)
- 196
Description
Using GA version.
1. Create a .NET Aspire Starter project in VS
2. Add packages in Api service project:
- Aspire.Npgsql.EntityFrameworkCore.PostgreSQL
- Microsoft.EntityFrameworkCore.Design
- Microsoft.EntityFrameworkCore.Tools
3. Create a DbContext in the api service
```
public class ApplicationDbContext(DbContextOptions options) : DbContext(options)
{
public DbSet Users { get; set; }
}
public class User
{
public int UserId { get; set; }
public required string Name { get; set; }
}
```
4. Create a postgres container and database in the apphost (Add Aspire.Hosting.PostgreSQL package)
```
var builder = DistributedApplication.CreateBuilder(args);
var postgresdb = builder.AddPostgres("postgres").AddDatabase("postgresdb");
var apiService = builder.AddProject("apiservice").WithReference(postgresdb);
builder.AddProject("webfrontend")
.WithExternalHttpEndpoints()
.WithReference(apiService);
builder.Build().Run();
```
5. Add a the following in the api service program.cs:
```
builder.AddNpgsqlDbContext("postgresdb");
```
6. Create a migration in the Api service using "dotnet ef migrations add initial"
7. Update the migration in the database fails with the following:
Build started...
Build succeeded.
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.HistoryRepository.GetAppliedMigrations()
at Npgsql.EntityFrameworkCore.PostgreSQL.Migrations.Internal.NpgsqlMigrator.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
I've tried with all possible locations of the migration assembly and so forth, same error.
Contributor guide
Assessment
This issue has not been assessed yet.