EF Core Code First with Identity differences between migrations on different machines

asp.net-identity c# ef-code-first entity-framework entity-framework-core

Question

We keep getting migrations that include changes to the ASP.NET Identity tables on one computer, but not on the other when a developer goes to create a migration.

We're using:

  • EF Core 3.1.3
  • .NET Identity Core
  • Package Manager Console Add-Migration <Name>
  • Visual Studio 2019 16.5.2
  • dotnet ef reports Entity Framework Core .NET Command-line Tools 3.1.3

We thought we had this fixed last time it started happening by:

  • Updating our EF Core Tools to the same versions
  • Updating Visual studio to the same versions
  • Deleting all migrations
  • Creating a new initial migration

But today it appeared again. On one machine Add-Migration wants to make these changes to the Identity tables while the other does not:

migrationBuilder.AlterColumn<string>(
    name: "Name",
    table: "AspNetUserTokens",
    maxLength: 128,
    nullable: false,
    oldClrType: typeof(string),
    oldType: "nvarchar(450)");

migrationBuilder.AlterColumn<string>(
    name: "LoginProvider",
    table: "AspNetUserTokens",
    maxLength: 128,
    nullable: false,
    oldClrType: typeof(string),
    oldType: "nvarchar(450)");

migrationBuilder.AlterColumn<string>(
    name: "ProviderKey",
    table: "AspNetUserLogins",
    maxLength: 128,
    nullable: false,
    oldClrType: typeof(string),
    oldType: "nvarchar(450)");

migrationBuilder.AlterColumn<string>(
    name: "LoginProvider",
    table: "AspNetUserLogins",
    maxLength: 128,
    nullable: false,
    oldClrType: typeof(string),
    oldType: "nvarchar(450)");
1
0
4/9/2020 5:07:33 PM

Popular Answer

I never found out how or why the default value was different between two machines, but I did find out how to override it in code. You can set the MaxLengthForKeys when registering Identity in Startup.cs:

services.AddDefaultIdentity<ApplicationUser>(options => {
    options.SignIn.RequireConfirmedAccount = true;
    options.Stores.MaxLengthForKeys = 128;
})
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();

There's also a related discussion about it here: https://github.com/dotnet/aspnetcore/issues/14503

1
4/9/2020 7:50:30 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow