dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.7k stars 3.17k forks source link

Update-Database generates correct migration but applies wrong one (even when forced to do so) #13008

Closed filenda closed 1 year ago

filenda commented 6 years ago

Error presented: Generated script seems to be using Initial migraiton instead of last one, even if forced to do so (see below)

Table 'Activities' already exists

Update-Database generates script not compatible with last migration. And Script-Migration --From LastMigrationName is empty

Migration expected to be run:

public partial class _20180815 : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "GymSubscriptionPlan",
                columns: table => new
                {
                    GymSubscriptionPlanId = table.Column<int>(nullable: false)
                        .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn),
                    Active = table.Column<bool>(nullable: false),
                    BillingPeriodization = table.Column<int>(nullable: false),
                    PagarMePlanCode = table.Column<string>(nullable: true),
                    PlanDetail = table.Column<string>(nullable: true),
                    PlanName = table.Column<string>(nullable: true),
                    Price = table.Column<double>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_GymSubscriptionPlan", x => x.GymSubscriptionPlanId);
                });

            migrationBuilder.CreateTable(
                name: "GymSubscription",
                columns: table => new
                {
                    GymSubscriptionId = table.Column<Guid>(nullable: false),
                    Active = table.Column<bool>(nullable: false),
                    CreateDate = table.Column<DateTime>(nullable: false),
                    GymId = table.Column<Guid>(nullable: false),
                    GymSubscriptionPlanId = table.Column<int>(nullable: false),
                    PagarMeSubscriptionId = table.Column<string>(nullable: true),
                    UpdateDate = table.Column<DateTime>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_GymSubscription", x => x.GymSubscriptionId);
                    table.ForeignKey(
                        name: "FK_GymSubscription_Gyms_GymId",
                        column: x => x.GymId,
                        principalTable: "Gyms",
                        principalColumn: "GymId",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_GymSubscription_GymSubscriptionPlan_GymSubscriptionPlanId",
                        column: x => x.GymSubscriptionPlanId,
                        principalTable: "GymSubscriptionPlan",
                        principalColumn: "GymSubscriptionPlanId",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_GymSubscription_GymId",
                table: "GymSubscription",
                column: "GymId");

            migrationBuilder.CreateIndex(
                name: "IX_GymSubscription_GymSubscriptionPlanId",
                table: "GymSubscription",
                column: "GymSubscriptionPlanId");
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "GymSubscription");

            migrationBuilder.DropTable(
                name: "GymSubscriptionPlan");
        }
    }

Steps to reproduce

Add the above migration to the Migrations folder of your project and run the following commands:

Update-Database -Migration "20180815" Script-Migration -From 20180815

Neither should word

Further technical details

EF Core version: 2.0.1 Database Provider: Pomelo.EntityFrameworkCore.Mysql (2.0.1) Operating system: Win10 x64 IDE: (e.g. Visual Studio 2017 15.4)

ajcvickers commented 6 years ago

@vfilenga The first case should execute all migrations that have not been applied from the initial upto and including the one specified. The second case should do nothing, since migrating from the last migration to the last migration is a no-op. If you are seeing behaviors different from these, then can you please post a runnable project/solution that demonstrates the behavior you are seeing?

filenda commented 6 years ago

@ajcvickers What is weird about the first case is that the last applied migration (see below)

the immediate next one is exactly the one I'm want it to apply

Select from database: image

Migrations folder: image

EDIT: Worked around it. Commented all "UP" methods from every previous migration in the folder to bypass them.

ajcvickers commented 6 years ago

@vfilenga We have not been able to reproduce this. Can you post a runnable project/solution that demonstrates the behavior you are seeing?

ajcvickers commented 6 years ago

EF Team Triage: Closing this issue as the requested additional details have not been provided and we have been unable to reproduce it.

BTW this is a canned response and may have info or details that do not directly apply to this particular issue. While we'd like to spend the time to uniquely address every incoming issue, we get a lot traffic on the EF projects and that is not practical. To ensure we maximize the time we have to work on fixing bugs, implementing new features, etc. we use canned responses for common triage decisions.