Skip to content

Model annotation changes absent from Down() method of initial migration #35016

Open
@roji

Description

@roji

When an initial migration is created with a model annotation, e.g. modelBuilder.HasDatabaseMaxSize("10") on SQL Server (see below for full repro), we get the following migration:

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.AlterDatabase()
        .Annotation("SqlServer:EditionOptions", "MAXSIZE = 10");

    migrationBuilder.CreateTable(
        name: "Blogs",
        columns: table => new
        {
            Id = table.Column<int>(type: "int", nullable: false)
                .Annotation("SqlServer:Identity", "1, 1"),
            Name = table.Column<string>(type: "nvarchar(max)", nullable: false)
        },
        constraints: table =>
        {
            table.PrimaryKey("PK_Blogs", x => x.Id);
        });
}

protected override void Down(MigrationBuilder migrationBuilder)
{
    migrationBuilder.DropTable(
        name: "Blogs");
}

Note that while there's an AlterDatabase operation in the Up() method, there's isn't one in the Down() method. If this is done later in a non-initial migration, everything works as expected:

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.AlterDatabase()
        .Annotation("SqlServer:EditionOptions", "MAXSIZE = 10");
}

protected override void Down(MigrationBuilder migrationBuilder)
{
    migrationBuilder.AlterDatabase()
        .OldAnnotation("SqlServer:EditionOptions", "MAXSIZE = 10");
}

It could be argued that the Down() method on the initial migration is, well, somewhat useless (and it kind of is, so this is pretty low priority); though we do have DropTable() and other things there.

Repro
await using var context = new BlogContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();

public class BlogContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseSqlServer("Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0;Encrypt=false")
            .LogTo(Console.WriteLine, LogLevel.Information)
            .EnableSensitiveDataLogging();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.HasDatabaseMaxSize("10");
    }
}

public class Blog
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Note: this is the cause of npgsql/efcore.pg#3183 and npgsql/efcore.pg#2514

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions