Skip to content

context.Database.Migrate() cannot create database #3701

@zhaozehong

Description

@zhaozehong

I am working with the latest .NET. using Visual Studio 2026.
Image
In my application, I would like to support several database provider, including PostgreSQL.

I can do Add-Migration.
Image

But, it cannot create database when I call context.DatabaseMigrate() in the source for postgreSQL, which works for all other database provider(I tested it with LocalDB, SQLServer, SQLite). Then I changed the server name from localhost to 127.0.0.1 as suggested, nothing changed.
Image

Next, I manually called Update-Database after Add-Migration operation in Package Manager Console, it works, the database really created. And all tables, including __EFMigrationsHistory, are created there.
Image
there is an error line in the picture but the database really created.

Next I put code to set default schema to public.
Image
then call Update-Database again(I have dropped the database created on last step), this time it failed.
Image

Ok, let's continue with last database created step.
After database created with Update-Database, I run my application, when it goes to context.Database.Migrate(), it show me
Image
I know 100% that there is no pending migration.(I called Update-Database, and there is only one migration existing and it must have been applied when Update-Database is called).
To verify,
Image

the pendingMigrations really has nothing.
I can jump context.Database.Migrate() to avoid the error message popup in the way shown in above picture.

Then I made another try,

  1. I create an empty database with pgAdmin (like customer do)
    Image
  2. I run the application, and the application will call context.Database.Migrate()
    Image
  3. I checked the Tables, nothing inside
    Image

NOTE:
context.Database.EnsureCreated() can always create database.
The problem for me is that, context.Database.Migrate() call doesn't really create the database. But I need to create database in this way.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions