重命名一个列,并通过EF Code First中的迁移更改列类型

时间:2018-03-04 11:23:09

标签: c# asp.net-mvc entity-framework ef-migrations

我是Asp的新手。 Net MVC 5架构,对实体框架代码优先迁移也不太了解。这是我通过迁移创建的表格,由于某些原因我只想将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");
            }
        }
    }

0 个答案:

没有答案