Skip to content

Migrations: Detect renamed properties - #6689

Merged
bricelam merged 1 commit into
dotnet:devfrom
bricelam:rename
Oct 6, 2016
Merged

bricelam merged 1 commit into
dotnet:devfrom
bricelam:rename

Conversation

@bricelam

@bricelam bricelam commented Oct 5, 2016

Copy link
Copy Markdown
Contributor

Resolves #6582

@ajcvickers ajcvickers left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@bricelam
bricelam merged commit af36d9d into dotnet:dev Oct 6, 2016
@bricelam
bricelam deleted the rename branch October 6, 2016 22:27
@thomaseyde

Copy link
Copy Markdown

I wonder, how can this possibly work? We have situations where we delete one property and add another one. These are not related, but EF still guess on a rename.

When the guess is incorrect, we end up with a database column with incorrect data.

@bricelam

Copy link
Copy Markdown
Contributor Author

We tried to be more conservative than EF6 where there were even more false positives.

Two things can help prevent false positives:

  • Make your migrations atomic--adding and removing several unrelated properties in a single migration should be rare.
  • Specify reasonable string lengths on properties. If two columns have different max lengths, they won't be matched.

@bricelam

Copy link
Copy Markdown
Contributor Author

We also encourage you to review the migrations for accuracy. The initial scaffold is just EF's best guess at your intent. Update it to reflect your actual intent.

@thomaseyde

Copy link
Copy Markdown

This is interesting, because this is not how migrations are marketed. Every article I have read just accept the generated migration,so it's not easy to recognize this is even possible. Not when we have the much debated snapshot-file (and designer-files) to consider as well. I never dared to fix the migration, because I thought I also had to update the snapshot.

@divega

divega commented Mar 26, 2018

Copy link
Copy Markdown
Contributor

@thomaseyde it would be great to know in what articles you got this impression, in case it is within our reach to improve their clarity in the future. FWIW, our documentation contains some more examples of when to modify migrations: https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/#adding-another-migration.

@thomaseyde

thomaseyde commented Mar 26, 2018 •

Copy link
Copy Markdown

Edit: I didn't recognize the article you referred to is the same as the first from my search. But my point stands. It's not clearly described that the generated migrations can or should be rewritten.

The very first article from this search show a simple workflow. Although the 2nd chapter "Add another migration" shows how to rewrite a migration, this information is easy to miss because it's not highlighted in any way.

The second article explains how to set up migrations, but nothing about rewriting them. It also points to the up/down methods and the snapshot file. The snapshot file looks like the end result of the migration, which lead us to believe we have to update the snapshot manually whenever we rewrite the suggested migration.

If this is not the case, why does the snapshot look like the end result? Shouldn't it represent the result from the last migration which this one builds upon?

The next results points to non-Microsoft resources, but all of them miss the point: The added migration is just a best guess:

Then a search for the snapshot file return an explanation that migrations should be removed with ´revert-migration´ so the snapshot also will be updated. This implies a manual edit of the migration will render the snapshot invalid.

So it's not only that the community seems to miss the fact that a generated migration is just a suggestion, but the tooling itself implies we shouldn't touch the result.

@bricelam

bricelam commented Mar 26, 2018 •

Copy link
Copy Markdown
Contributor Author

The EF Core article is meant to be read in entirety to understand how the Migrations feature works. You rarely have to review the initial migration since it's just creating the tables from scratch. If your model is correct, the tables will be correct. The places EF has to make educated guesses are when interpreting the differences between two models.

The section in the MVC article about the model snapshot could use some clarification. It doesn't need to show the actual code. I'm also not sure what point it's trying to make by describing these details of the Migrations implementation. (I've filed dotnet/AspNetCore.Docs#5801 to address this.)

For a better understand of the model snapshot, see the EF Core Migrations in Team Environments article. If you're not working in a team environment, you can mostly ignore the snapshot file.

The model snapshot loosely represents the database structure your model need in order to work. At the end of the day, EF doesn't care too much about how that structure is achieved. (e.g. data could be coming from either a table or a view; default values could be computed from default constraints or triggers; etc.) You're free to modify the migrations however you want so long as you satisfy the database structure. When you add another migration, EF looks at the difference between the snapshot and your current model to scaffold operations. If you haven't deviated from the expected structure, those new operations should still be perfectly valid even if you've altered the previous migration's operations.

the tooling itself implies we shouldn't touch the result

The reason we open the migration file after scaffolding it is to let you review and modify it. When we scaffold something that could potentially delete data, we further caution you with a warning to Please review the migration for accuracy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants