Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrations not created correctly #7196

Closed
joacar opened this issue Dec 7, 2016 · 6 comments
Closed

Migrations not created correctly #7196

joacar opened this issue Dec 7, 2016 · 6 comments

Comments

@joacar
Copy link

joacar commented Dec 7, 2016

Steps to reproduce

  1. Create a db context with a model
  2. Run dotnet ef migrations add initial && dotnet ef database update
  3. Add a column to the model that is not required
  4. Run dotnet ef migrations add new_column
  5. Run dotnet ef database update && dotnet ef database update

The issue

Get error message table '<model>' already exists. Inspecting the file <datetime>_new_column.cs reveals the answer - it tries to create the table again instead of just creating the correct alter command.

Side note: It would be better the have the 'migration name' pre-pended to the generated migration file since it would make it easier to work using the command line.

Further technical details

EF Core version: 1.1.0
Operating system: macOS Sierra 10.12.1
Visual Studio version: Visual Studio preview for Mac
MySQL version: 5.6.10
MySQL adapter: Sapient

Other details about my project setup:

@Allann
Copy link

Allann commented Dec 8, 2016

I can reproduce something similar. I have 3 DBContext classes in my project. when i perform (via PMC scaffold) add-migration MigrationName -context HistoryContext it adds the entities from the other contexts too. It therefore duplicates the CreateTable etc in this migration that are already performed in migrations from the other contexts.
HistoryContext.zip
20161208031200_AddFeeValueId.zip

The only thing I changed in the domain entity was add the FeeValueId column so the resulting migration should have been:

    public partial class AddFeeValueId : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<Guid>(
                name: "FeeValueId",
                schema: "History",
                table: "HistoryRateDetail",
                nullable: false,
                defaultValue: new Guid("00000000-0000-0000-0000-000000000000"));
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "FeeValueId",
                schema: "History",
                table: "HistoryRateDetail");
        }
    }

Is it safe to just delete the erroneous create table or does that mess up the changes made to the snapshot?

@joacar
Copy link
Author

joacar commented Dec 8, 2016

Creating a second migration and then calling dotnet ef migrations script 1 2 (where 1 and 2 are migrations that I created) doesn't yield the right result. It seem to ignore that there is a current value in the __EFMigrationHistory

Note Running Sapient MySQL and not sure how much of this logic is maintained in the core repo and what reside in Sapient.

@rowanmiller
Copy link
Contributor

@joacar could you share the model classes (or project) so that we can try to reproduce this. Migrations should use the ModelSnapshot.cs file that it generates to work out what changes since the last migration (rather than recreating the whole schema). I guess also just check that you didn't delete/move the snapshot file 😄.

And to answer a couple of your questions...

Creating a second migration and then calling dotnet ef migrations script 1 2 (where 1 and 2 are migrations that I created) doesn't yield the right result. It seem to ignore that there is a current value in the __EFMigrationHistory

Scripting doesn't pay any attention to the database. It creates a script based on the migrations you specify. This is because scripting is generally used to get a script to apply to a production database.

Side note: It would be better the have the 'migration name' pre-pended to the generated migration file since it would make it easier to work using the command line.

You can just use the short name (without the timestamp) when you run commands.

@rowanmiller
Copy link
Contributor

@Allann your issue seems to be different from the one being discussed here, so probably best to open a new issue. That said, I'm guessing types in one context have references to types in the other contexts? If so, they will get included in the model, and therefore in the migrations too. Take a look at #2725 for some discussion - and a feature we are planning that will help with this scenario.

@amglt
Copy link

amglt commented Aug 15, 2019

Same issue @joacar reported.

**Tech details **:
EF Core version: 3.0.0-preview8.19405.11
Operating system: MacOS Mojave 10.14.6
Visual Studio version: Visual Studio preview for Mac
SQL Server: Microsoft SQL Azure (RTM) - 12.0.2000.8

@ajcvickers
Copy link
Member

@ElLinn This is a closed issue from a long time ago. Please open a new issue and include a small, runnable project/solution or complete code listing that demonstrates the behavior you are seeing.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants