重命名列并通过 EF Code First 中的迁移更改列类型
Rename a column and also changing the column type through migration in EF Code First
我是 Asp 的新手。 Net MVC 5 架构,并且对 Entity Framework Code First Migrations 了解不多。这是我通过迁移创建的 table 的快照,由于某些原因,我只想将 ReleaseDate 列重命名为 ReleasedDate,并且还想将 Genre id 的列类型从 byte 更改为 int。是否可以通过迁移或任何其他替代方法来做到这一点?
Snap ot the Migration Of My Table
迁移代码:
namespace Vidly.Migrations
{
using System;
using System.Data.Entity.Migrations;
public partial class AddPropAndAnnotationsToMovie : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.Movies",
c => new
{
Id = c.Int(nullable: false, identity: true),
Name = c.String(nullable: false, maxLength: 255),
GenreId = c.Byte(nullable: false),
DateAdded = c.DateTime(nullable: false),
ReleaseDate = c.DateTime(nullable: false),
NumberInStock = c.Byte(nullable: false),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.Genres", t => t.GenreId, cascadeDelete: true)
.Index(t => t.GenreId);
CreateTable(
"dbo.Genres",
c => new
{
Id = c.Byte(nullable: false),
Name = c.String(nullable: false, maxLength: 255),
})
.PrimaryKey(t => t.Id);
}
public override void Down()
{
DropForeignKey("dbo.Movies", "GenreId", "dbo.Genres");
DropIndex("dbo.Movies", new[] { "GenreId" });
DropTable("dbo.Genres");
DropTable("dbo.Movies");
}
}
}
您可以撤消迁移、编辑 C# 实体并创建新的迁移。使用:
`Update-database -TargetMigration:0`
还原所有迁移,或者,
`Update-database -TargetMigration:"name of migration file"`
恢复到特定的迁移。 删除不需要的迁移文件。
然后 运行 Add-migration migration-name
更改了代码以创建新的迁移文件。
第三个选项是重命名代码中的字段,然后使用“Add-migration”。
避免编辑迁移文件。更改代码,运行一次迁移,更新数据库。
See also
我是 Asp 的新手。 Net MVC 5 架构,并且对 Entity Framework Code First Migrations 了解不多。这是我通过迁移创建的 table 的快照,由于某些原因,我只想将 ReleaseDate 列重命名为 ReleasedDate,并且还想将 Genre id 的列类型从 byte 更改为 int。是否可以通过迁移或任何其他替代方法来做到这一点?
Snap ot the Migration Of My Table
迁移代码:
namespace Vidly.Migrations
{
using System;
using System.Data.Entity.Migrations;
public partial class AddPropAndAnnotationsToMovie : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.Movies",
c => new
{
Id = c.Int(nullable: false, identity: true),
Name = c.String(nullable: false, maxLength: 255),
GenreId = c.Byte(nullable: false),
DateAdded = c.DateTime(nullable: false),
ReleaseDate = c.DateTime(nullable: false),
NumberInStock = c.Byte(nullable: false),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.Genres", t => t.GenreId, cascadeDelete: true)
.Index(t => t.GenreId);
CreateTable(
"dbo.Genres",
c => new
{
Id = c.Byte(nullable: false),
Name = c.String(nullable: false, maxLength: 255),
})
.PrimaryKey(t => t.Id);
}
public override void Down()
{
DropForeignKey("dbo.Movies", "GenreId", "dbo.Genres");
DropIndex("dbo.Movies", new[] { "GenreId" });
DropTable("dbo.Genres");
DropTable("dbo.Movies");
}
}
}
您可以撤消迁移、编辑 C# 实体并创建新的迁移。使用:
`Update-database -TargetMigration:0`
还原所有迁移,或者,
`Update-database -TargetMigration:"name of migration file"`
恢复到特定的迁移。 删除不需要的迁移文件。
然后 运行 Add-migration migration-name
更改了代码以创建新的迁移文件。
第三个选项是重命名代码中的字段,然后使用“Add-migration”。
避免编辑迁移文件。更改代码,运行一次迁移,更新数据库。
See also