EF Core修改Migration更新数据库表

场景

需要通过EF Migration修改数据库一张表的名称,字段,主键,索引

一般重命名表,生成的ef migration会删除旧表,创建一张新表,如果想不删除旧表,需要修改生成的migration

protected override void Up(MigrationBuilder migrationBuilder)
{
			migrationBuilder.RenameTable(
                name: "oldTableName",
                newName: "newTableName");

            migrationBuilder.DropPrimaryKey(
                name: "old_pk",
                table: "newTableName");

            migrationBuilder.AddPrimaryKey("new_pk", "newTableName", "columnName");

            migrationBuilder.RenameIndex(
                name: "old_indexName",
                newName: "new_indexName",
                table: "newTableName");
            
            migrationBuilder.RenameColumn("old_columnName", "newTableName", "new_columnName");

}

protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.RenameTable(
                name: "newTableName",
                newName: "oldTableName");

            migrationBuilder.DropPrimaryKey(
                name: "new_pk",
                table: "oldTableName");

            migrationBuilder.AddPrimaryKey("old_pk", "oldTableName", "coumnName");
            
            migrationBuilder.RenameIndex(
                name: "new_indexName",
                newName: "old_indexName",
                table: "oldTableName");
                
			migrationBuilder.RenameColumn("new_columnName", "oldTableName", "old_columnName");

        }

你可能感兴趣的:(.Net日常工作问题,数据库,c#)