Skip to content

EF Core 10: Migration code for discriminators with default name is created each time I add a new migration #37143

@SpaceOgre

Description

@SpaceOgre

Bug description

After upgrading to EF Core 10 and running dotnet ef migrations add I get a migration file that wants to change two of my Discriminator columns from string to int. But they allready are int, both in the database and in the snapshot.
This was not a problem with EF Core 9.

This only happens for discriminators that are using the defualt name: Discriminator if I change the name I get a migration with DropColumn + AddColumn but the next migration does not try and change the typ.

This is the migration I get:

    public partial class Test3 : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AlterColumn<int>(
                name: "Discriminator",
                schema: "Register",
                table: "Suppliers",
                type: "int",
                nullable: false,
                oldClrType: typeof(string),
                oldType: "nvarchar(max)");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AlterColumn<string>(
                name: "Discriminator",
                schema: "Register",
                table: "Suppliers",
                type: "nvarchar(max)",
                nullable: false,
                oldClrType: typeof(int),
                oldType: "int");
        }
    }

The snapshot looks like this:

                    b.Property<int>("Discriminator")
                        .HasColumnType("int");

                    b.HasDiscriminator();

If I manualy edit the snapshot to:

b.HasDiscriminator<int>("Discriminator");

Then it works once, as in the migration, does not try and change the value. But it reverts the snapshot change and the next migration again wants to change the discriminator type.

This is a big problem for us since we have test that verifies that no model changes are pending.

Could it somehow be connected to: #36564 ?

Your code

private sealed class EntityTypeConfiguration : IEntityTypeConfiguration<BaseSupplier>
    {
        public void Configure(EntityTypeBuilder<BaseSupplier> builder)
        {
            builder.ToSchema("Register");
            builder.Property(e => e.Name).NameConfiguration();
            builder.HasDiscriminator<SupplierDiscriminator>("Discriminator")
                .HasValue<ExternalSupplier>(SupplierDiscriminator.ExternalSupplier)
                .HasValue<OwnDirectionSupplier>(SupplierDiscriminator.OwnDirectionSupplier);
        }

        private enum SupplierDiscriminator
        {
            ExternalSupplier,
            OwnDirectionSupplier
        }
    }

Stack traces


Verbose output


EF Core version

10.0.0

Database provider

Microsoft.EntityFrameworkCore.SqlServer

Target framework

.Net 10

Operating system

Windows 11

IDE

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions