Migration generates redundant statements for optional arrays
.NET SDK: 7
Consider following snippet:
using Microsoft.EntityFrameworkCore;
Console.WriteLine("Hello World!");
class MyDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql();
base.OnConfiguring(optionsBuilder);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<MyEntity>(b => b.Property(x => x.Codes).HasDefaultValue(Array.Empty<string>()).IsRequired(false));
base.OnModelCreating(modelBuilder);
}
}
class MyEntity
{
public Guid Id { get; set; }
public string[] Codes { get; set; }
}
When I run initial migration then result is correct, but any subsequent migration will contain a redundant statement - one that does not introduce any change to database model. Also this statement is not reflected in any change in model snapshots.
Eg. generating migration again for this snippet without introducing any changes will result in:
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace PostgresEFMigrationOptionalArrayRepro.Migrations
{
/// <inheritdoc />
public partial class Empty : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<string[]>(
name: "Codes",
table: "MyEntity",
type: "text[]",
nullable: true,
defaultValue: new string[0],
oldClrType: typeof(string[]),
oldType: "text[]",
oldNullable: true,
oldDefaultValue: new string[0]);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<string[]>(
name: "Codes",
table: "MyEntity",
type: "text[]",
nullable: true,
defaultValue: new string[0],
oldClrType: typeof(string[]),
oldType: "text[]",
oldNullable: true,
oldDefaultValue: new string[0]);
}
}
}
Migration should be empty in this case. Problem exists only if there is .IsRequired(false) in the model builder.
Hi, can this issue get some attention?
I'll try to get around to this, but it may still take a while (as it's not a critical issue).
@ajcvickers does this ring a bell? I'm guessing there's a problem here with the comparison of the empty array default value, yielding a false negative which causes a migration to get detected. Am not sure if a value comparer is needed/used in the model differ for default values etc...