Code First Migrations to update the database

方法一:

1.程序包管理器控制台 输入指令:Enable-Migrations

注:

执行 Enable-Migrations 时可能会因为错误而打断,此时需要再次运行加参数的命令Enable-Migrations -Force:
Migrationshave already been enabled in project 'Mvc4DAL'. To overwrite the existingmigrations configuration, use the -Force parameter.

注意“Enable-Migrations”中间没有空格,“-Force”前面必须要有空格。

执行结束后项目中会新增一个文件夹,两个类分别是 1.InitialCreate 2.Configuration

2.设置 AutomaticMigrationsEnabled为 true 

打开Migrations文件夹中的 Configuration.cs,AutomaticMigrationsEnabled默认为 false 改为 true就哦了,否则将出现提示:
Unable to update database to match the current model because there arepending changes and automatic migration is disabled. Either write the pendingmodel changes to a code-based migration or enable automatic migration. SetDbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enableautomatic migration. You can use the Add-Migration command to write the pendingmodel changes to a code-based migration.

3.执行Update-Database

上述步骤都设置好了,打开“程序包管理器控制台”,运行命令 Update-Database,没有出错就大功成。这里要注意的是,数据库中有个名称为dbo.__MigrationHistory 的Table很重要,记录的是从创建数据库开始的全部更新的记录,所以在你没有绝对把握的情况下千万别动它。

 

 

 

转载于:https://www.cnblogs.com/liandy0906/p/8464139.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值