code first , Migration

文章引用至: https://www.cnblogs.com/panchunting/p/entity-framework-code-first-migrations.html

随着业务的增加, 之前code first创建的表可能已经不能满足需求, 比如增加一个字段, 这样就出来了‘Migrations

 第一步:

  • 在 Package Manager Console 下运行命令 Enable-Migrations

需要熟悉的命令有:

  •  Add-Migration 将 scaffold 创建下一次基于上一次迁移以来的更改的迁移;
  • Update-Databse 将任何挂起的迁移应用到数据库

《1》: 新增字段: ‘Url’

1. console输入: Add-Migration 'AddNewUrl'

2. 会看到产生一个新的cs文件

完善方法中的内容: (新增字段的类型需要自己定义, 如果为int型, 就是'c=>c.Int()')

 public partial class AddNewUrl : DbMigration
    {
        public override void Up()
        {
            AddColumn("dbo.News", "Url", c => c.String());
        }
        
        public override void Down()
        {
            DropColumn("dbo.News", "Url");
        }
    }

 《2》 删除字段

扫描二维码关注公众号,回复: 5135617 查看本文章

猜你喜欢

转载自www.cnblogs.com/zxhome/p/10343146.html