Programmatic Scaffold-Migration Broken After Updating from 8.0.15 to 9.0.4
- Dominant language
- C#
- Stars
- 14.8k
- Forks
- 3.4k
- PR merge metrics
- PR metrics pending
Description
### Bug description
project: [ProgrammaticalScaffold.zip](https://github.com/user-attachments/files/19980137/ProgrammaticalScaffold.zip)
1- run Update-Database to apply the first migration.
2- try to scaffold a new migration (one column added) by clicking on the scaffold button in the Home page.
Expected result:
A new migration script file that only contains AddColumn (the newly added column).
Actual result:
A new migration file containing a complete model snapshot instead of just the incremental change.
### Your code
```csharp
public async Task ScaffoldMigration()
{
try
{
var assemblyTypes = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.FullName.Contains($"WebApplication"))?.GetTypes();
var dbContextType = assemblyTypes?.FirstOrDefault(a => a.FullName == $"WebApplication.ApplicationDbContext");
if (dbContextType is null)
{
return;
}
var dbContext = HttpContext.RequestServices.GetService(dbContextType) as DbContext;
var relationalDatabaseCreator = dbContext.GetService() as IRelationalDatabaseCreator;
if (!await relationalDatabaseCreator.ExistsAsync() || dbContext.Database.HasPendingModelChanges())
{
var migrations = dbContext.Database.GetMigrations();
var services = new ServiceCollection().AddEntityFrameworkDesignTimeServices().AddDbContextDesignTimeServices(dbContext);
var designTimeServices = new SqlServerDesignTimeServices();
designTimeServices.ConfigureDesignTimeServices(services);
var scaffolder = services.BuildServiceProvider().GetRequiredService();
var migrationPath = Path.Combine(_hosting.ContentRootPath, "Migrations");
Directory.CreateDirectory(migrationPath);
var nextMigrationNumber = 3;
var migration = scaffolder.ScaffoldMigration($"Migration-{nextMigrationNumber:D3}", $"WebApplication", $"Migrations");
await Task.WhenAll(
System.IO.File.WriteAllTextAsync(Path.Combine(migrationPath, migration.MigrationId + migration.FileExtension),
migration.MigrationCode),
System.IO.File.WriteAllTextAsync(Path.Combine(migrationPath, migration.MigrationId + ".Designer" + migration.FileExtension),
migration.MetadataCode),
System.IO.File.WriteAllTextAsync(Path.Combine(migrationPath, migration.SnapshotName + migration.FileExtension),
migration.SnapshotCode)
);
}
}
catch { }
}
```
### Stack traces
```text
```
### Verbose output
```text
```
### EF Core version
9.0.4
### Database provider
SqlServer
### Target framework
.NET 9
### Operating system
Windows 11
### IDE
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.