Next create an initial migration by typing Add-Migration Initial. Open the Package Manager Console in Visual Studio and select the project that contains your DbContext (!important) and type Enable-Migrations: The next steps involve using the Nuget Package Manager Console to re-enable migrations and create an initial migration. If there are any schema errors you will encounter them at runtime… Recreating the Migrations Because there's no migrations table in the database and there are no migrations in your project EF just pretends that everything is in sync and runs. If you now recompile and run your application again you're likely going to find that your application will run just fine. If you don't care about the initial code you can delete the file or the entire Migrations folder. If you leave it you might have to check if you need to update any initial data loading code to reflect the potentially updated schema. You can leave the Configuration.cs file, as it may contain initial data adding code. This folder contains code files for each schema modification that was made with Up() and Down() methods that add and remove a given migration. Your project that contains the DbContext for your EF application contains a Migrations folder. Note however, that if you remove the table and run your application it will run, as EF simply won't check if the schema matches. If this table exists EF checks to see whether the latest migration has been applied and if it hasn't fails and throws an error to the effect that the database and EF schema are out of sync. Go ahead and delete the _MigrationHistory table which tells EF what migrations have been applied. The first step is to remove the migrations table: The migrations got corrupted because the database is shared amongst multiple applications and the migration history was hopelessly bonked. I recently ran into this problem with a simple example database that I use for various applications. You've now essentially reset the schema to the latest version.Īgain if you had custom code in your old migrations that added custom constraints or modified data alongside of the generated Migration code you may have to add this code back in the initial migration generated.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |