适合初步使用ef数据迁移的新人,有更好的办法希望能指出来并告诉楼主,互相学习
普通的数据库迁移执行三条命令
(0)Enable-Migrations(打开数据迁移)
(1)Add-Migration InitialCreate
(2) Update-Database -Verbose(自动迁移只需要执行这个)
如果只是修改了字段,执行这些命令会提示
Table 'XXXXXXXXXXXXXXXXXXX' already exists
![](http://upload-images.jianshu.io/upload_images/9919202-f657234a1d5e50cc.png?imageMogr2/auto-orient/strip|imageView2/2/w/606/format/webp)
表示这个迁移会执行建表操作,已存在导致迁移失败。
楼主解决方法如下:
输入 get-help entityframeworkcore
获取指令帮助。
![](http://upload-images.jianshu.io/upload_images/9919202-b44b098b101e5965.png?imageMogr2/auto-orient/strip|imageView2/2/w/854/format/webp)
使用 Script-Migration Generates a SQL script from migrations.(脚本迁移从迁移生成SQL脚本。)
![](http://upload-images.jianshu.io/upload_images/9919202-6f6df46e6f818d4f.png?imageMogr2/auto-orient/strip|imageView2/2/w/766/format/webp)
执行
![](http://upload-images.jianshu.io/upload_images/9919202-3e021a778043d867.png?imageMogr2/auto-orient/strip|imageView2/2/w/170/format/webp)
执行完毕会弹出迁移所用的sql语句,并且有时间记录
![](http://upload-images.jianshu.io/upload_images/9919202-42d893a7245a3d4d.png?imageMogr2/auto-orient/strip|imageView2/2/w/747/format/webp)
放到sql工具泡一下就好啦
![](http://upload-images.jianshu.io/upload_images/9919202-b210a21e02bfb3e3.png?imageMogr2/auto-orient/strip|imageView2/2/w/657/format/webp)
本文出处:https://www.jianshu.com/p/d1776fe34e9e。为了记录和收藏,做了搬运工。
其实每次修改后 都进行 add-migration versions 之后update-database versions 即可(不是add一次可以update多次,是每次update之前都要add)