Skip to content

ef migrations add keep add the same AlterColumn migration content for strongly-typed primary key #30699

@Cologler

Description

@Cologler

To Reproduce

Create a new project and install packages:

  • Microsoft.EntityFrameworkCore.Design
  • Microsoft.EntityFrameworkCore.Sqlite

Then add some codes:

public class TheDbContext : DbContext
{
    public DbSet<TheEntity> TheEntities { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<TheEntity>()
            .Property(e => e.Id)
            .HasConversion(new TheId.EntityFrameworkCoreValueConverter());
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
        => optionsBuilder.UseSqlite(":memory:");
}

public record TheEntity
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public TheId Id { get; set; }
}

public record struct TheId(int Value)
{
    public class EntityFrameworkCoreValueConverter : ValueConverter<TheId, int>
    {
        public EntityFrameworkCoreValueConverter() : this(default) { }

        public EntityFrameworkCoreValueConverter(ConverterMappingHints? mappingHints = default)
            : base(e => e.Value, value => new(value), mappingHints)
        {
        }
    }
}

Now, every time I call dotnet ef migrations add (New-Guid).ToString().Substring(0, 8), it will generated the same content:

        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AlterColumn<int>(
                name: "Id",
                table: "TheEntities",
                type: "INTEGER",
                nullable: false,
                oldClrType: typeof(int),
                oldType: "INTEGER")
                .OldAnnotation("Sqlite:Autoincrement", true);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AlterColumn<int>(
                name: "Id",
                table: "TheEntities",
                type: "INTEGER",
                nullable: false,
                oldClrType: typeof(int),
                oldType: "INTEGER")
                .Annotation("Sqlite:Autoincrement", true);
        }

Include provider and version information

EF Core version: 7.0.5
Database provider: Microsoft.EntityFrameworkCore.Sqlite
Target framework: .NET 7.0
Operating system: Windows 11 x64
IDE: Visual Studio 2022 17.5.4

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions