Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In EFCore, how do I scaffold my context and then start doing migrations?

I want to do code first development on an existing SQL Server Database. I have used the Scaffold-DbContext command to generate the entities that I want into the tables I want. That's great. But, there were previously code first migrations done to this database. So I deleted the __EFMigrationsHistory table in the SQL Database. Now I want to start doing migrations. Unfortunately, when I run Add-Migration, it generates migration code to generate all of the tables again. I don't understand how I am supposed to tell it that these tables already exist. When I am reverse engineering, how do I generate the migration of the stuff it's scaffolding for me in the existing database?

like image 274
tnk479 Avatar asked Oct 29 '25 17:10

tnk479


1 Answers

Code First Migrations uses a snapshot of the model stored in the most recent migration to detect changes to the model (you can find detailed information about this in Code First Migrations in Team Environments).

Source here.

Run Add-Migration InitialCreate –IgnoreChanges to create the initial migration from an existing database. Then update-database to simply add the migration to the _EFMigrationsHistory table.

After that, you are good to go brother.

like image 186
Dennis VW Avatar answered Oct 31 '25 10:10

Dennis VW



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!