Ludvig W
Ludvig W

Reputation: 824

EF Core 5 creates tables twice in migrations

I've created a project and a migration, but it seems like the migrations does not care about previous migrations when adding new ones, that is, the migration files just tries to create the same tables twice.

I created two migrations, First and Second. What changed was I added a string property to a Post entity (not shown here). What I expected was something like the following:

migrationBuilder.AddColumn(...)

But instead I got a Second migration containing everything from the First migration but when creating the table Post, it had the added column. It's almost like it did not even care there was a First migration thus acting like its the first migration ever.

First

public partial class First : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.CreateTable(
            name: "AspNetRoles",
            columns: table => new
            {
                Id = table.Column<string>(type: "nvarchar(450)", nullable: false),
                Name = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: true),
                NormalizedName = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: true),
                ConcurrencyStamp = table.Column<string>(type: "nvarchar(max)", nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_AspNetRoles", x => x.Id);
            });
    ...

Second

public partial class Second : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.CreateTable(
            name: "AspNetRoles",
            columns: table => new
            {
                Id = table.Column<string>(type: "nvarchar(450)", nullable: false),
                Name = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: true),
                NormalizedName = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: true),
                ConcurrencyStamp = table.Column<string>(type: "nvarchar(max)", nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_AspNetRoles", x => x.Id);
            });
    ...

As you can see they are identical, why is this happening?

Upvotes: 3

Views: 1890

Answers (1)

Ludvig W
Ludvig W

Reputation: 824

As it turns out for some reason visual studio for mac played tricks on me again. The following lines was added to my csproj file.

<Compile Remove="Migrations\MainDbContextModelSnapshot.cs" />
<Compile Remove="Migrations\20210102195131_Test1.Designer.cs" />
<Compile Remove="Migrations\20210102195131_Test1.cs" />

Now it all makes sense why it tried to create everything from scratch again because there was no snapshot. Anyway, if this strange thing happens to anyone else you might want to have a look in your csproj files for this. I can't explain why these are added.

The solution is simply to remove such lines if you see them and everything should work as expected.

Upvotes: 10

Related Questions