ASP - 启动时 Core Migrate EF Core SQL DB

问题描述:

是否可以让我的 ASP Core Web API 确保使用 EF Core 将数据库迁移到最新迁移?我知道这可以通过命令行完成,但我想以编程方式完成.

Is it possible to have my ASP Core Web API ensure the DB is migrated to the latest migration using EF Core? I know this can be done through the command line, but I want to do it programatically.

可以使用

db.Database.EnsureCreated();

使您的数据库与您当前的模型保持同步.如果要启用迁移(如果怀疑后续迁移),则使用

to get your db up to date with your current model. If you want to enable migrations (If subsequent migrations are suspected), then use

db.Database.Migrate();

并随着时间的推移进行后续迁移.

and put your subsequent migrations over time.