更改列类型中的实体框架迁移错误

时间:2018-03-04 13:27:29

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

我刚刚在我的一个类中将我的实体从int更改为byte并添加了迁移。当我在包管理器控制台中运行update-database命令时出现以下错误:

Error Number:5074,State:1,Class:16
The index 'IX_GenreId' is dependent on column 'GenreId'.
The object 'FK_dbo.Movies_dbo.Genres_GenreId' is dependent on column 'GenreId'.
ALTER TABLE DROP COLUMN GenreId failed because one or more objects access this column.

如何解决此问题?

P.S:My Genre类是名为Movie的类的成员。

类型课程:

namespace Vidly.Models
{
    public class Genre
    {
        public byte Id { get; set; } //changed from public int Id{get;set;}
        [Required]
        [StringLength(255)]
        public string Name { get; set; }
    }
}

电影课:

namespace Vidly.Models
{
    public class Movie
    {
        public int Id { get; set; }


        [Required]
        [StringLength(255)]
        public string Name { get; set; }


        [Required]
        public Genre Genre { get; set; }
        public byte GenreId { get; set; }

        public DateTime DateAdded { get; set; }
        public DateTime ReleasDate { get; set; }
        public int NumberinStock { get; set; }

    }
}

迁移代码:

   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 个答案:

没有答案