In a .NET 8 project using EF Core 8, I’ve noticed that when creating a migration to remove a column from a temporal entity table, several SQL Server-specific annotations are automatically included in the generated migration code. These annotations seem to be redundant. Here is an example:
public class Order
{
public int OrderId { get; set; }
public DateTime OrderDate { get; set; }
public List<OrderRow> OrderRows { get; set; }
}
public class OrderRow
{
public int OrderRowId { get; set; }
public int OrderId { get; set; }
public Order Order { get; set; }
public string ProductName { get; set; }
public int Quantity { get; set; }
}
public class AppDbContext : DbContext
{
public DbSet<Order> Orders { get; set; }
public DbSet<OrderRow> OrderRows { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=(localdb)\MSSQLLocalDB; Database=EfCoreTest; Integrated Security=true");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Configure temporal table for Orders
modelBuilder.Entity<Order>()
.ToTable("Orders", b => b.IsTemporal(t =>
{
t.HasPeriodStart("PeriodStart");
t.HasPeriodEnd("PeriodEnd");
t.UseHistoryTable("OrderHistory");
}));
// Configure temporal table for OrderRows
modelBuilder.Entity<OrderRow>()
.ToTable("OrderRows", b => b.IsTemporal(t =>
{
t.HasPeriodStart("PeriodStart");
t.HasPeriodEnd("PeriodEnd");
t.UseHistoryTable("OrderRowHistory");
}));
}
}
After creating the database I remove ProductName from the OrderRow entity and create a migration:
public partial class RemoveProductName : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "ProductName",
table: "OrderRows")
.Annotation("SqlServer:IsTemporal", true)
.Annotation("SqlServer:TemporalHistoryTableName", "OrderRowHistory")
.Annotation("SqlServer:TemporalHistoryTableSchema", null)
.Annotation("SqlServer:TemporalPeriodEndColumnName", "PeriodEnd")
.Annotation("SqlServer:TemporalPeriodStartColumnName", "PeriodStart");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "ProductName",
table: "OrderRows",
type: "nvarchar(max)",
nullable: false,
defaultValue: "")
.Annotation("SqlServer:IsTemporal", true)
.Annotation("SqlServer:TemporalHistoryTableName", "OrderRowHistory")
.Annotation("SqlServer:TemporalHistoryTableSchema", null)
.Annotation("SqlServer:TemporalPeriodEndColumnName", "PeriodEnd")
.Annotation("SqlServer:TemporalPeriodStartColumnName", "PeriodStart");
}
}
Is the inclusion of these annotations by EF Core intentional? What I can see the outcome of the migration is the same even if I don’t include them.