【问题标题】:ASP.net core 2.0 MVC every new database migrations includes data from previous migrationsASP.net core 2.0 MVC 每个新的数据库迁移都包含以前迁移的数据
【发布时间】:2018-04-25 03:52:03
【问题描述】:

您好,我在添加新模型或编辑旧模型时尝试进行数据库迁移时遇到问题。我正在使用 EntityFrameworkCore。问题是每个新的迁移都包含前一个的 Up 方法的代码。

/* migrationBuilder.DropIndex( 名称:“用户名索引”, 表:“AspNetUsers”);

        migrationBuilder.DropIndex(
            name: "IX_AspNetUserRoles_UserId",
            table: "AspNetUserRoles");

        migrationBuilder.DropIndex(
            name: "RoleNameIndex",
            table: "AspNetRoles");

        migrationBuilder.AddColumn<DateTime>(
            name: "Birthdate",
            table: "AspNetUsers",
            nullable: false,
            defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));

        migrationBuilder.AddColumn<string>(
            name: "FirstName",
            table: "AspNetUsers",
            nullable: true);

        migrationBuilder.AddColumn<string>(
            name: "LastName",
            table: "AspNetUsers",
            nullable: true);
            */

        migrationBuilder.CreateTable(
            name: "Articles",
            columns: table => new
            {
                Id = table.Column<int>(nullable: false)
                    .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                AuthorId = table.Column<string>(nullable: true),
                Content = table.Column<string>(maxLength: 25000, nullable: false),
                PublishDate = table.Column<DateTime>(nullable: false),
                Title = table.Column<string>(maxLength: 150, nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Articles", x => x.Id);
                table.ForeignKey(
                    name: "FK_Articles_AspNetUsers_AuthorId",
                    column: x => x.AuthorId,
                    principalTable: "AspNetUsers",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Restrict);
            });

在这里,我正在尝试将新模型文章迁移到数据库,但是该方法包含以前迁移中的一些内容,当我尝试更新数据库时,出现以下错误: “无法删除索引 'AspNetUserRoles.IX_AspNetUserRoles_UserId',因为它不存在或您没有权限。” 如果我注释了之前迁移中的代码,并且只为适当的模型保留数据,则它会成功迁移。但随后我添加了一个新模型“Supplement”,并通过在 Up 方法中添加的 Article 迁移得到相同的结果:

/* migrationBuilder.DropIndex( 名称:“用户名索引”, 表:“AspNetUsers”);

        migrationBuilder.DropIndex(
            name: "IX_AspNetUserRoles_UserId",
            table: "AspNetUserRoles");

        migrationBuilder.DropIndex(
            name: "RoleNameIndex",
            table: "AspNetRoles");

        migrationBuilder.AddColumn<DateTime>(
            name: "Birthdate",
            table: "AspNetUsers",
            nullable: false,
            defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));

        migrationBuilder.AddColumn<string>(
            name: "FirstName",
            table: "AspNetUsers",
            nullable: true);

        migrationBuilder.AddColumn<string>(
            name: "LastName",
            table: "AspNetUsers",
            nullable: true);
            */

        migrationBuilder.CreateTable(
            name: "Articles",
            columns: table => new
            {
                Id = table.Column<int>(nullable: false)
                    .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                AuthorId = table.Column<string>(nullable: true),
                Content = table.Column<string>(maxLength: 25000, nullable: false),
                PublishDate = table.Column<DateTime>(nullable: false),
                Title = table.Column<string>(maxLength: 150, nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Articles", x => x.Id);
                table.ForeignKey(
                    name: "FK_Articles_AspNetUsers_AuthorId",
                    column: x => x.AuthorId,
                    principalTable: "AspNetUsers",
                    principalColumn: "Id",
                    onDelete: ReferentialAction.Restrict);
            });

        migrationBuilder.CreateTable(
            name: "Supplements",
            columns: table => new
            {
                Id = table.Column<int>(nullable: false)
                    .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                Description = table.Column<string>(nullable: true),
                Name = table.Column<string>(nullable: true),
                Price = table.Column<decimal>(nullable: false),
                Quantity = table.Column<int>(nullable: false),
                SupplementType = table.Column<int>(nullable: false),
                inStock = table.Column<bool>(nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Supplements", x => x.Id);
            });

我该如何解决这个问题?

【问题讨论】:

  • 你有没有想过这个问题?我也遇到了同样的问题。
  • 从来没有弄明白。我所做的是删除/评论已经在旧迁移中的字段。另一种方法是删除所有较旧的迁移,只保留最新的迁移和所有数据。

标签: c# database asp.net-core entity-framework-core


【解决方案1】:

另一种选择是确保映射正确,只需删除 up/down 函数即可获得干净的状态:

https://github.com/aspnet/EntityFrameworkCore/issues/11763

【讨论】:

    猜你喜欢
    • 2021-10-25
    • 1970-01-01
    • 2017-05-26
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 2020-05-07
    • 2021-06-04
    • 1970-01-01
    相关资源
    最近更新 更多