Ef——code frist

1.创建项目(注意路径不能有中文)

 

 

 

 创建成功以后会生成

 

 

 2.创建数据迁移

1、数据迁移=>工具=>Get New 程序包=>程序包控制台(3个命令)(enable-migrations)
1)、enable-migrations 启动数据迁移 (将默认的那个文件的AutomaticMigrationsEnabled 改成 true)
2)、add-migration (into) 迁移版本名称 随便起
3)、 update-database 更新数据
4)、添加字段重新迁移 add-migration (迁移名称重新随便起

 

 

 每次修改以后会自动生成

 

 

踩坑注意:1.EF版本一定要最新版本。默认安装因为有个中文包的原因,EF版本是6.2.0,执行enable-migrations 会报错   System.ArgumentNullException: 值不能为 null。

                  2.路径一定不能有中文,不然创建的Migrations 文件加载不到项目里面,会报错

3.再次执行  add-migration (迁移名称重新随便起 报错

Unable to generate an explicit migration because the following explicit migrations are pending: [202207290523260_test001]. Apply the pending explicit migrations before attempting to generate a new explicit migration.

解决方案,删除原来那个

      

posted @ 2022-07-29 15:30  乌柒柒  阅读(396)  评论(0编辑  收藏  举报