-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Programmatic Scaffold-Migration Issue After Updating to 9.0.0 #35146
Comments
This issue is lacking enough information for us to be able to fully understand what is happening. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate. |
here is my full scaffold code that worked perfectly at version 8.0.11:
the exception happens at |
I'm not sure why you scaffold a new migration when the database doesn't exist, but try replacing using (var DbContext = Activator.CreateInstance(MainDbContext, DbContextOptionsBuilderObject.Options, HttpContextAccessor) as DbContext)
{
var relationalDatabaseCreator = DbContext.GetService<IDatabaseCreator>() as IRelationalDatabaseCreator;
var databaseExists = await relationalDatabaseCreator.ExistsAsync();
if (!databaseExists || DbContext.Database.HasPendingModelChanges())
{
var services = new ServiceCollection().AddEntityFrameworkDesignTimeServices().AddDbContextDesignTimeServices(DbContext);
var designTimeServices = new SqlServerDesignTimeServices();
designTimeServices.ConfigureDesignTimeServices(services);
var serviceProvider = services.BuildServiceProvider();
var scaffolder = serviceProvider.GetRequiredService<IMigrationsScaffolder>();
var migrationPath = Path.Combine(Hosting.ContentRootPath, @"..\", SystemName, "Migrations", DbContext.UseSqlServerProvider ? "SqlServer" : "NpgSql");
if (!Directory.Exists(migrationPath)) Directory.CreateDirectory(migrationPath);
var migration = scaffolder.ScaffoldMigration($"Migration-test1", $"Company.Systems.{SystemName}", $"Migrations.{(DbContext.UseSqlServerProvider ? "SqlServer" : "NpgSql")}");
await System.IO.File.WriteAllTextAsync(Path.Combine(migrationPath, migration.MigrationId + migration.FileExtension), migration.MigrationCode);
await System.IO.File.WriteAllTextAsync(Path.Combine(migrationPath, migration.MigrationId + ".Designer" + migration.FileExtension), migration.MetadataCode);
await System.IO.File.WriteAllTextAsync(Path.Combine(migrationPath, migration.SnapshotName + migration.FileExtension), migration.SnapshotCode);
result.Add(new(true, "Main DbContext Scaffolded"));
}
} |
I've used your code but am still encountering the error. I’d like to debug it myself. Could you let me know which EF Core projects I should reference? |
👀 |
any due date? |
any workarounds please? cause my migration to .NET 9 is stopped due to that issue. Thanks |
@oONoobMasterOo I am still unable to repro this issue locally. But you can try adding a new migration manually before running the code. |
Hi, During the initial migration without a snapshot, it works well, but subsequent migrations result in the same error. However, performing the using the standard command line works without issues. would like to know which project to reference to debug this issue in my environment. regards. |
ScaffoldMigration.zip |
You'd need to reference EFCore.Design, EFCore.PG and all the projects they reference. Setting up a debugging environment will not be straightforward. |
I see, this happens because you call this before scaffolding a migration: if (_appDbContext.Database.GetPendingMigrations().Any())
{
_appDbContext.Database.Migrate();
} #35432 Will fix this. |
cool, when this fix gets released? |
It will be in EF Core 10 preview releasing in a couple of months. |
After updating Npgsql to version 9.0.0, I encounter the following exception while attempting to scaffold migrations programmatically:
my scaffold code:
The text was updated successfully, but these errors were encountered: