Skip to content
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

Regression issue where command line migrations with -o updates the snapshot in the project #23828

Closed
theo-albers opened this issue Jan 7, 2021 · 3 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@theo-albers
Copy link

File a bug

Up to EF Core 3.x the behavior was like this:

  • in your C# project folder where you have EF Core migrations
  • dotnet ef migrations add SecondVersion -o c:\temp
  • in the folder c:\temp we now have:
    • SecondVersion.cs
    • SecondVersion.designer.cs
    • MyDbContextModelSnapshot.cs
  • in the project folder nothing is changed

With EF Core 3.x I can rerun "dotnet ef migrations add SecondVersion" without the -o option and it will add the exact same migration to my project.

In EF Core 5.x the behavior is like this:

  • in your C# project folder where you have EF Core migrations
  • dotnet ef migrations add SecondVersion -o c:\temp
  • in the folder c:\temp we now have:
    • SecondVersion.cs
    • SecondVersion.designer.cs
  • in the project folder the model snapshot is changed
    • MyDbContextModelSnapshot.cs

With EF Core 5.x I can rerun "dotnet ef migrations add SecondVersion" without the -o option and it will add an empty migration to my project, because the model snapshot is already changed.

I expect the -o option to respect the output path and not to touch my local model snapshot file. This allows me to easily check my migration, change the code, redo, etc. until I'm satisfied with the outcome.

Run run-testcase.cmd in the sample project to see the behavior.
EfCoreSample.zip

@AndriySvyryd
Copy link
Member

@bricelam

@bricelam
Copy link
Contributor

bricelam commented Jan 8, 2021

This is the result of fixing #13342. The migration generated by dotnet ef migrations add must be added to the project.

Here are some options for previewing a migration:

  1. Use dotnet ef migrations remove to remove it and generate a new, corrected one
  2. Use version control to revert the changes and generate a new one
  3. Use the underlying APIs to build your own tool:
     var db = new MyDbContext();
    
     var serviceCollection = new ServiceCollection();
     serviceCollection.AddEntityFrameworkDesignTimeServices();
     serviceCollection.AddDbContextDesignTimeServices(db);
     var serviceProvider = serviceCollection.BuildServiceProvider();
    
     // Scaffold a migration
     var migrationsScaffolder = serviceProvider.GetService<IMigrationsScaffolder>();
     var migration = migrationsScaffolder.ScaffoldMigration(migrationName, rootNamespace);
    
     File.WriteAllText(migrationFile, migration.MigrationCode, Encoding.UTF8);
     File.WriteAllText(migrationMetadataFile, migration.MetadataCode, Encoding.UTF8);
     File.WriteAllText(modelSnapshotFile, migration.SnapshotCode, Encoding.UTF8);

@theo-albers
Copy link
Author

theo-albers commented Jan 12, 2021

Thanks for listing the alternatives. I guess I have to learn a new habit and undo the snapshot file when using the -o option. I can see the need for the #13342 fix in case you really want to use the -o option to place the migrations in a specific folder in your project.

@ajcvickers ajcvickers added the closed-no-further-action The issue is closed and no further action is planned. label Jan 12, 2021
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

4 participants