剑轩

EF Code First常用命令

电脑版发表于:2019/5/8 10:45


Enable-Migrations启用数据库迁移

Enable-Migrations –EnableAutomaticMigrations 启动自动迁移


Add-Migration Name(名字可以随便取):相当于记录一个版本变更记录,例如修改了字段名字,改短了长度等

Add-Migration Initial -Force:可以初始


Update-Database 更新到数据库

Update-Database -Verbose :更新到数据库,并且查看所执行的Sql语句 


Update-Database –TargetMigration:name(版本记录名称)    更新数据库到特定的版本


启用自动迁移

 如果你想你部署的应用程序在启动的时候自动升级数据库,你可以通过注册数据库初始化MigrateDatabaseToLatestVersion做到这一点。

 将Configuration构造函数的设置为:AutomaticMigrationsEnabled=true,AutomaticMigrationDataLossAllowed=true.

public Configuration() {
    AutomaticMigrationsEnabled = true;
    AutomaticMigrationDataLossAllowed = true;
}

在Global.asax的 Application_Start 方法中添加以下代码:

Database.SetInitializer(new MigrateDatabaseToLatestVersion<AppContext, Configuration>());





关于TNBLOG
TNBLOG,技术分享。技术交流:群号677373950
ICP备案 :渝ICP备18016597号-1
App store Android
精彩评论
{{item.replyName}}
{{item.content}}
{{item.time}}
{{subpj.replyName}}
@{{subpj.beReplyName}}{{subpj.content}}
{{subpj.time}}
猜你喜欢