Generating non-nullable rowversion on SQL Server with EF Core 3.1

2

We are trying to generate a non-nullable rowversion column on SQL Server with EF Core 3.1 using Fluent API:

public class Person
{
    public int Id { get; set; }
    public byte[] Timestamp { get; set; }
}

public class PersonEntityConfiguration : IEntityTypeConfiguration<Person>
{
    public void Configure(EntityTypeBuilder<Person> builder)
    {
        builder.HasKey(p => p.Id);

        builder.Property(p => p.Timestamp)
            .IsRowVersion()
            .IsRequired();
    }
}

This works fine when the entire table is new:

public partial class PersonMigration : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.CreateTable(
            name: "Persons",
            columns: table => new
            {
                Id = table.Column<int>(nullable: false)
                    .Annotation("SqlServer:Identity", "1, 1"),
                Timestamp = table.Column<byte[]>(rowVersion: true, nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Persons", x => x.Id);
            });
    }
}

However, we sometimes need to add the rowversion to an existing table. In that case, EF Core generates an invalid migration:

public partial class PersonTimestampMigration : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.AddColumn<byte[]>(
            name: "Timestamp",
            table: "Persons",
            rowVersion: true,
            nullable: false,
            defaultValue: new byte[] {  });
    }
}

The default value generated above will cause an exception when being applied to the database:

Failed executing DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']

ALTER TABLE [Persons] ADD [Timestamp] rowversion NOT NULL DEFAULT 0x;

Microsoft.Data.SqlClient.SqlException (0x80131904): Defaults cannot be created on columns of data type timestamp. Table 'Persons', column 'Timestamp'.
Could not create constraint or index. See previous errors.

Is this a known bug in EF Core? The issue can be fixed by manually removing the defaultValue: new byte[] { } from the migration, but is there a way of suppressing the default value from being generated using the Fluent API?

c#
.net-core
entity-framework-core
ef-code-first
ef-fluent-api
asked on Stack Overflow Sep 2, 2020 by User14207973 • edited Sep 2, 2020 by marc_s

1 Answer

1

Is this a known bug in EF Core?

It is bug/defect for sure, but probably not known, since it's happening even in the latest at this time EF Core 5.0 preview. Or is known, but with low priority (for them) - you have to check EF Core Issue Tracker.

Tried adding explicitly .HasDefaultValue(null) and .HasDefaultValueSql(null) - nothing helps, so the only option is manually removing the defaultValue: new byte[] { } from the migration. The good thing is that when you do so, it works and the column is created and populated successfully even though the table has existing records (which is the reason EF Core adds such defaultValue argument for new required columns in general, but as we see shouldn't do that for ROWVERSION).

answered on Stack Overflow Sep 2, 2020 by Ivan Stoev

User contributions licensed under CC BY-SA 3.0