title: Entity Framework Core: Unable to run migration script on production server, The maximum key length for a clustered index is 900 bytes
body: <p>I'm trying to deploy my ASP.NET Core website on a production server, hosted with Plesk. I have 2 projects in my solution: 1 containing my <code>DbContext</code>, 1 containing my web project.</p>
  
  <p>I generated migrations for my <code>DbContext</code>:</p>
  
  <pre><code>dotnet ef migrations add AddIdentity --project ..\MyProject.Data
  </code></pre>
  
  <p>Then I generated a production migration script as mentioned in the docs (<a href="https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet#dotnet-ef-migrations-script" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet#dotnet-ef-migrations-script</a>)</p>
  
  <pre><code>dotnet ef migrations script --idempotent --output "MigrationScripts\AddIdentity.sql" --context MyDbContext --project ..\MyProject.Data
  </code></pre>
  
  <p>Then I navigated to the SQL utility on my production server (MyLittleAdmin) and pasted the generated SQL script in it. This succeeds but I get the following error:</p>
  
  <blockquote>
    <p>Warning! The maximum key length for a clustered index is 900 bytes. The index 'PK_AspNetUserLogins' has maximum length of 1800 bytes. For some combination of large values, the insert/update operation will fail.</p>
    
    <p>Warning! The maximum key length for a clustered index is 900 bytes. The index 'PK_AspNetUserTokens' has maximum length of 1804 bytes. For some combination of large values, the insert/update operation will fail.</p>
  </blockquote>
  
  <p>If ignoring this error, I would probably get in trouble later on with queries failing for "no apparent reason" (this reason).</p>
  
  <p>My code is very straightforward:</p>
  
  <pre><code>internal class MintPlayerContext : IdentityDbContext&lt;User, Role, int&gt;
  {
      private readonly IConfiguration configuration;
      public MintPlayerContext(IConfiguration configuration) : base()
      {
          this.configuration = configuration;
      }
  
      protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
      {
          base.OnConfiguring(optionsBuilder);
          optionsBuilder.UseSqlServer(configuration.GetConnectionString("MintPlayer"), options =&gt; options.MigrationsAssembly("MintPlayer.Data"));
      }
  
      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
          base.OnModelCreating(modelBuilder);
      }
  }
  
  internal class User : IdentityUser&lt;int&gt;
  {
      public string PictureUrl { get; set; }
  }
  
  internal class Role : IdentityRole&lt;int&gt;
  {
  }
  </code></pre>
  
  <p>I also tried using Guid's (I want to use Guids eventually) but even with int's MyLittleAdmin complaints about the Key being too long.
  I already checked on Google but this only gives diagnostic articles and does not really provide a solution.</p>
  
  <p>Right now this is the AspNetUsers section of the migration:</p>
  
  <pre><code>CREATE TABLE [AspNetUsers] (
      [Id] int NOT NULL IDENTITY,
      [UserName] nvarchar(256) NULL,
      [NormalizedUserName] nvarchar(256) NULL,
      [Email] nvarchar(256) NULL,
      [NormalizedEmail] nvarchar(256) NULL,
      [EmailConfirmed] bit NOT NULL,
      [PasswordHash] nvarchar(max) NULL,
      [SecurityStamp] nvarchar(max) NULL,
      [ConcurrencyStamp] nvarchar(max) NULL,
      [PhoneNumber] nvarchar(max) NULL,
      [PhoneNumberConfirmed] bit NOT NULL,
      [TwoFactorEnabled] bit NOT NULL,
      [LockoutEnd] datetimeoffset NULL,
      [LockoutEnabled] bit NOT NULL,
      [AccessFailedCount] int NOT NULL,
      [PictureUrl] nvarchar(max) NULL,
      CONSTRAINT [PK_AspNetUsers] PRIMARY KEY ([Id])
  );
  </code></pre>
  
  <p>What can I do to solve this?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>I tried modifying my DbContext configuration to force the Key to be only the Id:</p>
  
  <pre><code>protected override void OnModelCreating(ModelBuilder modelBuilder)
  {
      base.OnModelCreating(modelBuilder);
  
      modelBuilder.Entity&lt;User&gt;().HasKey(u =&gt; u.Id);
      modelBuilder.Entity&lt;Role&gt;().HasKey(r =&gt; r.Id);
  }
  </code></pre>
  
  <p>This seems to affect the migration indeed:</p>
  
  <pre><code>[Id] int NOT NULL IDENTITY,
  </code></pre>
  
  <p>Instead of:</p>
  
  <pre><code>[Id] uniqueidentifier NOT NULL,
  </code></pre>
  
  <p>But it still gives the same error</p>
  
  <p><strong>Edit: Adding the AspNetUserLogins, AspNetUserRoles, stripped the if's</strong></p>
  
  <pre><code>CREATE TABLE [AspNetUserLogins] (
      [LoginProvider] nvarchar(450) NOT NULL,
      [ProviderKey] nvarchar(450) NOT NULL,
      [ProviderDisplayName] nvarchar(max) NULL,
      [UserId] int NOT NULL,
      CONSTRAINT [PK_AspNetUserLogins] PRIMARY KEY ([LoginProvider], [ProviderKey]),
      CONSTRAINT [FK_AspNetUserLogins_AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [AspNetUsers] ([Id]) ON DELETE CASCADE
  );
  
  CREATE TABLE [AspNetUserTokens] (
      [UserId] int NOT NULL,
      [LoginProvider] nvarchar(450) NOT NULL,
      [Name] nvarchar(450) NOT NULL,
      [Value] nvarchar(max) NULL,
      CONSTRAINT [PK_AspNetUserTokens] PRIMARY KEY ([UserId], [LoginProvider], [Name]),
      CONSTRAINT [FK_AspNetUserTokens_AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [AspNetUsers] ([Id]) ON DELETE CASCADE
  );
  </code></pre>