question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Does EF Core ignore migrations file with old `product version`

See original GitHub issue

I migrated my .NET Core 1.1 app over to .NET Core 2 and have issues with the ef db migration. I noticed that the new migration I added when my app is running .NET Core 2 seems to not have knowledge of my .NET Core 1.1 migrations. Is this because the new file has a product version of .HasAnnotation("ProductVersion", "2.0.0-rtm-26452") Where as my old ones were: .HasAnnotation("ProductVersion", "1.1.2")?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:22 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
carlos8kcommented, Jul 4, 2018

I have a same problem when migrating my project from core 1.1 to core 2.1 and I found a solution that might help other developers who encountered the same problem.

The reason for the new EF to eliminate tables that do not exist and to create tables that already exist is in the old ForSqlServerToTable method. This method (deprecated in this version, overridden by the ToTable method) has generated this code in ModelSnapshot.cs:

modelBuilder.Entity("Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole", b =>
                {
                    b.Property<string>("Id")
                        .ValueGeneratedOnAdd();

                    b.Property<string>("ConcurrencyStamp")
                        .IsConcurrencyToken();

                    b.Property<string>("Name")
                        .HasMaxLength(256);

                    b.Property<string>("NormalizedName")
                        .HasMaxLength(256);

                    b.HasKey("Id");

                    b.HasIndex("NormalizedName")
                        .IsUnique()
                        .HasName("RoleNameIndex");

                    b.ToTable("AspNetRoles");

                    b.HasAnnotation("SqlServer:TableName", "Roles");
                });

The new ToTable method doesn`t create “b.HasAnnotation(“SqlServer:TableName”, “Roles”);” line and not recognizes this. In other words, the new implementation consider “AspNetRoles” as current table name and then attempts to change the AspNetRoles to Roles, for example.

For workaround this behavior, I change my ModelSnapshot class manualy, removing “b.HasAnnotation(“SqlServer:TableName”, “Roles”);” line, and change “b.ToTable(“AspNetRoles”);” to “b.ToTable(“Roles”);”.

Now my code looks like this:

modelBuilder.Entity("Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityRole", b =>
                {
                    b.Property<string>("Id")
                        .ValueGeneratedOnAdd();

                    b.Property<string>("ConcurrencyStamp")
                        .IsConcurrencyToken();

                    b.Property<string>("Name")
                        .HasMaxLength(256);

                    b.Property<string>("NormalizedName")
                        .HasMaxLength(256);

                    b.HasKey("Id");

                    b.HasIndex("NormalizedName")
                        .IsUnique()
                        .HasName("RoleNameIndex");

                    b.ToTable("Roles");
                });

That way, I was able to perform new migrations and the generated code did not want to drop and create tables.

1reaction
ajcvickerscommented, Apr 24, 2018

@p2atran Probably the best way would be to scaffold a model from the database and compare the model created from that with the one that you have. (Probably easiest way to diff would be to add an empty migration after doing the scaffolding the model and then use the model snapshot file for the diff.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Does EF Core ignore migrations file with old product ...
The reason for the new EF to eliminate tables that do not exist and to create tables that already exist is in the...
Read more >
Why does EF 6 ignore applied migrations if I move the ...
EF does already know where the migrations live. It tells me I can't add a new one because the old ones are still...
Read more >
Managing Migrations - EF Core
You are free to move Migrations files and change their namespace manually. New migrations are created as siblings of the last migration.
Read more >
Entity Framework Core Migrations
With migrations, you can easily apply or revert database changes as your application evolves, without losing existing data. EF Core Migrations ...
Read more >
Migrations and Seed Data With Entity Framework Core
EF Core provides a method called Migrate to execute migration actions for us. All we have to do is to create model classes,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found