EntityFramework Core production migrations

痴心易碎 提交于 2020-07-10 05:38:33

问题


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:

  1. generation from the model
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!