Bug #121043 EF Core 10 idempotent migration script contains invalid MySQL syntax
Submitted: 30 Jul 19:53
Reporter: nuozhen Yang Email Updates:
Status: Open Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:MySql.EntityFrameworkCore 10.0.7 OS:Any
Assigned to: CPU Architecture:Any
Tags: Connector/Net, EntityFrameworkCore, idempotent script, migrations

[30 Jul 19:53] nuozhen Yang
Description:
`MySql.EntityFrameworkCore` 10.0.7 generates SQL Server-style control flow when an idempotent EF Core migration script is requested. The generated script contains top-level `IF NOT EXISTS (...) BEGIN ... END` blocks.

That syntax is not valid in MySQL. MySQL flow-control statements use `IF ... THEN ... END IF` and are available inside stored programs, not as top-level statements in a SQL batch. As a result, the command completes successfully and writes a script, but MySQL cannot execute it.

The generated script should be executable by MySQL and should apply each migration only when its row is absent from `__EFMigrationsHistory`.

Script generation was reproduced with:

- .NET SDK 10.0.302
- Microsoft.EntityFrameworkCore.Design 10.0.10
- dotnet-ef 10.0.10
- MySql.EntityFrameworkCore 10.0.7

The repository pins the optional execution check to MySQL Server 8.4.10.

The issue is independent of the model. A single table and a single initial migration are enough to reproduce it.

Minimal reproduction repository: https://github.com/yangnuozhen/MySQL-idempotent-issue

How to repeat:
Clone the reproduction repository and run:

```shell
dotnet tool restore
dotnet build
dotnet tool run dotnet-ef -- migrations script --idempotent --project src/MySqlIdempotentRepro --no-build --output artifacts/idempotent.sql
```

No database connection is required for script generation.

The generated script includes:

```sql
START TRANSACTION;
IF NOT EXISTS(SELECT * FROM `__EFMigrationsHistory` WHERE `MigrationId` = '20260730192706_InitialCreate')
BEGIN
    CREATE TABLE `Widgets` (
        `Id` int NOT NULL AUTO_INCREMENT,
        `Name` longtext NOT NULL,
        PRIMARY KEY (`Id`)
    );
END;
```

Execute the script against MySQL 8.4.10. The repository includes a Docker Compose file that does this during server initialization:

```shell
docker compose up --abort-on-container-exit
```

MySQL rejects the first generated `IF NOT EXISTS` statement with error 1064. The same invalid pattern is also used for the insert into `__EFMigrationsHistory`.

Suggested fix:
Generate MySQL-compatible conditional migration SQL for `--idempotent`, without emitting SQL Server `IF ... BEGIN ... END` batch syntax. Please add a provider test that generates an idempotent script with at least one migration and executes it against MySQL.