问题
How do I update the database for production? I've moved the connection string secrets out of the application. Our CI/CD pipeline handles the token replacement within the connection string "template" that remains in the appsettings, and developers won't have production database access.
I had expected to use automatic migrations, only to discover today that Microsoft eliminated that for EF Core.
Will I have to, every time, temporarily overwrite the connection string that normally specifies my local dev copy of the database? It seems that way, but it also seems very janky process to me, so I wonder if I'm missing something.
回答1:
There seem to be a confusion of what automatic migration means.
Migrations consist of two parts:
- generation from the model
- applying to the database.
Migration generation in turn could be two types:
- automatic - at runtime, current model is compared to model produced by the last migration, and if there are changes, it generates an automatic migration code.
- manual - migrations are generated at design time using the design time tools (
Add-Migration
command). The auto-generated code can be modified. Modified or not, the migration is stored along with the application code.
Regardless of how migrations are generated, they can be applied at design time using the tools (Update-Database
command) and/or at runtime.
What EF6 supports and EF Core have removed is the auto-generation. EF Core migrations cannot be generated at runtime - they must be generated at design time and stored with the application code.
Applying them at runtime is achieved with Migrate
method called from some application startup point:
dbContext.Database.Migrate();
For more info, see Migrations and Apply migrations at runtime EF Core documentation topics.
来源:https://stackoverflow.com/questions/57000291/entityframework-core-production-migrations