I tried adding context.Database.Migrate() to the Configure method of Startup.cs underneath DbInitializer.Initialize(context); and this didn't cause a problem when I ran it on my machine but when I deployed it I got "500 Internal Server Error: An error occurred while starting the application."
According to your description, I followed the tutorial as you mentioned to test this issue. I added the DbInitializer.cs
in this section and add context.Database.Migrate()
under Configure method of Startup.cs
as follows:
DbInitializer.Initialize(context);
context.Database.Migrate();
I added a new property named IdNo
for Student class, then started the web application, I could encounter the following error:

Note: If the Students
table has no any records, the DbInitializer.cs
would add feed records, then I encounter the above error.
Here is the summary about DatabaseFacade.EnsureCreated()
and DatabaseFacade.Migrate()
:
DatabaseFacade.EnsureCreated()
Ensures that the database for the context exists. If it exists, no action is
taken. If it does not exist then the database and all its schema are created.
If the database exists, then no effort is made to ensure it is compatible with
the model for this context.
Note that this API does not use migrations to create the database. In addition,the database that is created cannot be later updated using migrations. If you are targeting a relational database and using migrations, you can use the DbContext.Database.Migrate() method to ensure the database is created and all migrations are applied.
DatabaseFacade.Migrate()
Applies any pending migrations for the context to the database. Will create the database if it does not already exist. Note that this API is mutually exclusive with DbContext.Database.EnsureCreated().
EnsureCreated does not use migrations to create the database and therefore the
database that is created cannot be later updated using migrations.
Based on your scenario, you need to leverage migrations feature to update your database schema. As I known, EF Core does not support automaic migrations, you could follow this similar case and this git issue. You could use context.Database.Migrate()
instead of context.Database.EnsureCreated()
in your DbInitializer.cs
and manually add migration file via add-migration
, and commit the created migration file to your GitHub repository.

Note: You need to take care of the seed data in DbInitializer.cs
, in order to avoid inserting the same record.