简体   繁体   English

asp.net 显示指定密钥的核心 2 太长; 最大密钥长度为 3072 字节

[英]asp.net core 2 showing specified key was too long; max key length is 3072 bytes

I am working on .net core 2. I am using mysql as database.我正在研究 .net 核心 2。我正在使用 mysql 作为数据库。 When i am writing the following code dbContext.Database.EnsureCreated();当我编写以下代码时dbContext.Database.EnsureCreated(); it is generating me an error它给我带来了一个错误

MySql.Data.MySqlClient.MySqlException: 'Specified key was too long; MySql.Data.MySqlClient.MySqlException: '指定的密钥太长; max key length is 3072 bytes'最大密钥长度为 3072 字节'

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ApplicationDbContext dbContext)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            // ===== Create tables ======
            dbContext.Database.EnsureCreated();

            app.UseMvc();
        }

I am able to generate roleclaims, roles, userclaims, users successfully.我能够成功生成角色声明、角色、用户声明和用户。 I am using applicationDbcontext which is extending IdentityDbContext.我正在使用扩展 IdentityDbContext 的 applicationDbcontext。

How can this error be resolved.如何解决此错误。 need help thanks in advance.需要帮助提前致谢。

I also face this issue what i do is 我也面对这个问题,我要做的是

  1. Open command prompt and go to project location then run following command 打开命令提示符并转到项目位置,然后运行以下命令

    dotnet ef migrations script -o initial.sql dotnet ef迁移脚本-o initial.sql

  2. It will generate sql, open the file and remove all VARCHAR(767) with VARCHAR(100) 它将生成sql,打开文件并使用VARCHAR(100)删除所有VARCHAR(767)

run the sql in sqlyog/workbench you first migration will be created & now you can continue work. 在sqlyog / workbench中运行sql,将创建第一次迁移,现在就可以继续工作。

and for rest of migrations make sure do not make primary key varchar for greater length or use int 对于其余的迁移,请确保不要使主键varchar更长,也不要使用int

Thanks 谢谢

Not a fix, but a simple workaround when Irfan's answer does not apply.不是修复,而是 Irfan 的回答不适用时的简单解决方法。

After receiving this error, I addressed the potential issue of keys from combined fields exceeding the limit by setting their max sizes in the migration Up method - this did not help.收到此错误后,我通过在迁移 Up 方法中设置最大大小来解决组合字段中的键超出限制的潜在问题 - 这没有帮助。

I generated a script in Package Manager Console with script-migration -o initial.sql -context MyDbContextName and saw that there were no exceptional varchars in the stored migrations.我在 Package 管理器控制台中使用script-migration -o initial.sql -context MyDbContextName生成了一个脚本,发现存储的迁移中没有异常的 varchars。

I gave up on a code solution and did this: -我放弃了代码解决方案并这样做了:-

  1. In MySQL Workbench, create the empty schema (database) by right-clicking in an empty part of the Schemas tab and choosing Create Schema.在 MySQL Workbench 中,通过右键单击“架构”选项卡的空白部分并选择“创建架构”来创建空架构(数据库)。
  2. In Package Manager Console, run update-database -context ApplicationDbContext .在 Package 管理器控制台中,运行update-database -context ApplicationDbContext

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM