4

I know it's against the purpose of migration, but it doesn't make sense to me to make a new migration file for every single changes that I do to the model.

I start with enabling migration, then initial the migration and visual studio make a 201502151515_initial.cs

i do some changes in the 201502151515_initial.cs file and go for update-database.

then I understand I have some bug in the model. then if i try to update-database it will fail and show me this error:

Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration. You can use the Add-Migration command to write the pending model changes to a code-based migration.

if I try to add-migration, then it will create another file for me

is it anyway to update the current 201502151515_initial.cs file?

I can do it manually that is a hell of task to make sure everything is fine. I can delete the current file and create it again, in this case i have inconsistency with database(because I already update-database). or find a better way to update the current file.

is it any better way to do it?

1
  • 1
    The only thing you should change in the generated migration file is data manipulation (Sql function and such). If you don't like how EF generated the model difference, you need to change your model - entities, configurations, etc. Then when you're happy with those changes, either delete the unapplied migration or run Add-Migration <same name, no dates> -Force to re-scaffold the migration. Note, that this will completely overwrite the file. It essentially just skips the delete and keeps the date part of the file the same.
    – gligoran
    Feb 20, 2016 at 17:17

2 Answers 2

0

Maybe you would prefer automatic migrations?. This is similar to the solution you are looking for.

You don't need to add migrations, just push to the database

Enable-Migrations –EnableAutomaticMigrations
Update-Database
1
  • unfortunately in ASP .NET Core 1.1 Enable-Migration is not supported: Enable-Migrations is obsolete. Use Add-Migration to start using Migrations.
    – MushyPeas
    Dec 20, 2016 at 14:54
-1

in dotnet core it becomes easy.

just dotnet ef migrations remove then dotnet ef migrations add init

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.