Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update the database snapshot to match the database after changes have been made to the model

Using Entity FrameWork 6.1.3. and Code First.

I'm currently working in my own branch, but using a development database. Another developer has been working in the main branch and has applied some migrations to the development database after I had branched. I made some changes to some of the models in my branch by adding extra fields; the tables for these models I modified already exist on the database (apart from 1 new model).

The current snapshot in my branch is not what the actual database schema currently is. Normally I would do a Add-Migration Merge -IgnoreChanges to bring me up to date and then make my changes. (Correct?)

The problem is that I have changes I have made that I still need to be applied to the database and doing the above command ignores these changes. The code generated when I scaffold a new migration is trying to undo (drop/alter) all of the migrations that were applied on the main branch, and it also currently sees all of the models that I have made changes to (and some other models) as new tables and is trying to create the new tables instead of adding the new columns.

What is the best way to solve this and is it possible to avoid situations like this in the first place?

I was thinking that I could merge the main branch into my branch to get the migrations, but wasn't sure if that would solve my problem.

Any migrations generated will also need to work when applied to our production database.

Hope I've explained this well enough. I'm still quite new to EF so any help will be appreciated.

like image 730
T27M Avatar asked Oct 28 '25 07:10

T27M


1 Answers

What happened to me today was that I basically had the whole Migrations folder missing, so no Migration files, no Snapshot files, nothing. I wanted to do a little update on the DB (add a new column on a table). So I've followed these steps and eventually got it working (commands in Package Manager Console):

  1. Add-Migration temp - This will generate a Migration file (which would recreate the whole database) and the Snapshot file. Apparently this Snapshot file corresponds to the current database state;
  2. Deleted the Migration file, not by typing Remove-Migration (because this would delete the Snapshot file as well), but simply by deleting the file directly (in the Solution Explorer, for example). At this point I had the current Snapshot and no Migrations;
  3. Add-Migration new_column_etc - By adding this migration, since I've had a Snapshot for it to compare to, it was actually well generated and was only showing the changes I did (and what I intended to update in the DB in the first place);
  4. Update-Database - Now I have my database updated and the Snapshot file corresponds to the latest version.

I know it may deviate a bit from what was originally asked, but in desperate situations it can provide a way to re-generate the Snapshot file to the current version of the DB.

like image 132
Pedro Magalhães Avatar answered Oct 29 '25 22:10

Pedro Magalhães



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!