I recently started to work on a .NET project using Entity Framework (5.0.11). This project already has 5 registered migrations, and after creating a new model (+ register it to DbContext
), I generated a new migration.
Here is my new model class:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Myproject.Database
{
public class CompanyData
{
public int Id { get; set; }
public Company Company{ get; set; }
public int CustomDataId { get; set; }
}
}
And here is the migration created when I run
dotnet ef migrations add M6
public partial class M6 : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "CompanyDatas",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
CompanyId = table.Column<int>(type: "int", nullable: true),
CustomDataId = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_CompanyData", x => x.Id);
table.ForeignKey(
name: "FK_CompanyDatas_tCompany_CompanyId",
column: x => x.CompanyId,
principalTable: "tCompany",
principalColumn: "com_id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateIndex(
name: "IX_CompanyDatas_CompanyId",
table: "CompanyDatas",
column: "CompanyId");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "CompanyDatas");
}
}
The migration file seems to be good, since it reflects the changes I’ve made when creating my model.
However, when I run dotnet ef database update
to update my database, Entity Framework tries to execute all the migrations from the beginning.
After checking in my database, I could see that there are the following registered migrations.
MigrationId ProductVersion
----------------------------------
20240219103430_M1 5.0.11
20240219105713_M2 5.0.11
20240305074220_M3 5.0.11
20240311150824_M4 5.0.11
20240319153547_M5 5.0.11
But when I run dotnet ef migrations list --verbose
, the terminal outputs the following information :
20240219103430_M1 (Pending)
20240219105713_M2 (Pending)
20240305074220_M3 (Pending)
20240311150824_M4 (Pending)
20240319153547_M5 (Pending)
20240925150433_M6 (Pending)
I don’t understand why M1 to M5 migrations appear as pending since they are in the [__EFMigrationsHistory]
table.
Thank you in advance for any help.
saperlipopette987 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.