ASP - 启动时 Core Migrate EF Core SQL DB [英] ASP - Core Migrate EF Core SQL DB on Startup

查看:21
本文介绍了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.

这篇关于ASP - 启动时 Core Migrate EF Core SQL DB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆