Entity Framework Core:无法在生产服务器上运行迁移脚本,聚集索引的最大密钥长度为900字节

问题描述 投票:0回答:2

我正在尝试将我的ASP.NET Core网站部署在由Plesk托管的生产服务器上。我的解决方案中有2个项目:1个包含我的DbContext,1个包含我的Web项目。

我为我的DbContext生成了迁移:

dotnet ef migrations add AddIdentity --project ..\MyProject.Data

然后我生成了文档(https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet#dotnet-ef-migrations-script)中提到的生产迁移脚本

dotnet ef migrations script --idempotent --output "MigrationScripts\AddIdentity.sql" --context MyDbContext --project ..\MyProject.Data

然后,我导航到生产服务器(MyLittleAdmin)上的SQL实用程序,并将生成的SQL脚本粘贴到其中。这成功,但是出现以下错误:

警告!聚集索引的最大密钥长度为900字节。索引“ PK_AspNetUserLogins”的最大长度为1800个字节。对于大值的某些组合,插入/更新操作将失败。

警告!聚集索引的最大密钥长度为900字节。索引“ PK_AspNetUserTokens”的最大长度为1804字节。对于大值的某些组合,插入/更新操作将失败。

如果忽略此错误,稍后可能由于“没有明显原因”(此原因)而使查询失败。

我的代码非常简单:

internal class MintPlayerContext : IdentityDbContext<User, Role, int>
{
    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 => options.MigrationsAssembly("MintPlayer.Data"));
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }
}

internal class User : IdentityUser<int>
{
    public string PictureUrl { get; set; }
}

internal class Role : IdentityRole<int>
{
}

我也尝试过使用Guid的(我最终想使用Guid的),但是即使int的MyLittleAdmin抱怨Key太长。我已经在Google上进行了检查,但这仅提供诊断性文章,并没有真正提供解决方案。

现在是迁移的AspNetUsers部分:

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])
);

我该怎么解决?

编辑:

我试图修改我的DbContext配置以强制将密钥仅作为ID:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<User>().HasKey(u => u.Id);
    modelBuilder.Entity<Role>().HasKey(r => r.Id);
}

这似乎确实影响了迁移:

[Id] int NOT NULL IDENTITY,

而不是:

[Id] uniqueidentifier NOT NULL,

但是它仍然给出相同的错误

编辑:添加AspNetUserLogins,AspNetUserRoles,删除了if的内容

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
);

我正在尝试将我的ASP.NET Core网站部署在由Plesk托管的生产服务器上。我的解决方案中有2个项目:1个包含我的DbContext,1个包含我的Web项目。我生成了...

c# asp.net-identity plesk entity-framework-core-3.1
2个回答
0
投票

AspNetUserLoginsAspNetUserTokens需要定义一个较短的键。我假设在模型中未定义任何键,因此所有列均已添加到键中。也许包括一些大的字符串列。


0
投票

您可以尝试将其更改为:

© www.soinside.com 2019 - 2024. All rights reserved.