ASP.net core 2.0 MVC every new database migrations includes data from previous migrations

asp.net-core c# database entity-framework-core

Question

Hello, I have encountered a problem trying to make database migrations when adding new models or editing old ones. I am using EntityFrameworkCore. The problem is that every new migration contains the code from the Up method of the previous one.

/* migrationBuilder.DropIndex( name: "UserNameIndex", table: "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);
            });

Here i am trying to migrate a new model Article to the database, but the method contains some things from the previous migration and when i try to update the database i get the following error: "Cannot drop the index 'AspNetUserRoles.IX_AspNetUserRoles_UserId', because it does not exist or you do not have permission." If i comment the code from the previous migration and just leave the data for the appropriate model it migrates successfully. But then i add a new model "Supplement" and get the same thing with the Article migration added in the Up method:

/* migrationBuilder.DropIndex( name: "UserNameIndex", table: "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);
            });

How can i fix this?

1
2
12/8/2017 7:30:32 AM

Popular Answer

Another alternative is to make sure the mappings are correct and just delete the up/down functions to have a clean state:

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

0
4/24/2018 7:52:03 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow