EF Core Model Snapshot out of sync between add-migration and update
Asked Answered
S

1

5

I'm using EF Core and code migrations. It's my understanding that the snapshot file is supposed to be the "target database" to build future migration files off of.

Situation:

1) I did an add-migration to create a migration after some changes. This updated the modelsnapshot which I guess is the expected behavior.
2) I then realized I realized I mis-typed one of my fields. Instead of a string it was supposed to be a byte[] 3) I made the changes to the classes 4) I did add-migration again.

Now the DataSnapshot seems to be out of sync because the new add-migration does not include this type file change.

It doesn't seem like I can do a remove-migration because no migration was actually applied to any database.

I know in EF6, this was a fine way to do it because it always targeted an actual database for the changes and not this snapshot file. So just deleting the change file and remaking the change file was good enough.

This is the second time I've had to make some change to a migration file after realizing there was an issue (without actually running the update against anything) and both times were just incredible pains to try to fix the issue without wiping out all my previous migrations and starting over.

My question is how do I handle this now? Should I be deleting both the change file and the snapshot file every time I do backtrack on an add migration?

I'm really concerned that I'm missing something here because now I have to make sure that not only is the change file correct but also the current snapshot is correct as well and if that snapshot ever goes out of sync, then I have to go and manually update it to

Swedenborgianism answered 22/4, 2019 at 20:33 Comment(0)
F
6

Probably have solved this by now, but I just ran into this.

Not sure if this is exactly the same issue, but this is how I understand it.

update-database is a DB action and updates your DB to current version or target migration.

remove-migration is an EF action and updates your migration stack and updates your snapshot.

To downversion I had to update database then remove migration down to the intended target.

At this point adding a new migration gave me the expected set of changes, where as before it was giving me none.

This appeared to do what I expected from EF doing update-database and update-database -target

The only thing I think you can actually do wrong in this case is delete your migration files before doing the update, because the down steps are indicated in the migration files, so it wont be able to complete this step if you remove the files. If you have the files, you can freely target migrations to set your current DB state.

I believe the confusing part is that the snapshot was previously used on every migration, where as now there's one snapshot for "last migration"

Furrier answered 5/8, 2019 at 16:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.