身份(在数据库创建)错误指定的键太长 [英] Identity (on database creation) error specified key was too long

查看:367
本文介绍了身份(在数据库创建)错误指定的键太长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有ASPNET身份和MySQL的一个问题,这个问题是错误,实在是堵我,这个错误是以下内容:

I have here an issue with AspNet Identity and MySql, the issue is error which is really blocking me, this error is following:

指定键太长最大密钥长度为767字节

specified key was too long max key length is 767 bytes

现在我跟着这个教程使用MySQL与身份:

Now I followed this tutorial of using MySql with Identity:

http://www.asp.net/mvc/tutorials/security/aspnet-identity-using-mysql-storage-with-an-entityframework-mysql-provider

我想说我有进展,因为现在至少我得到的MySQL产生了一些表,但我在这个问题上的堆栈,很长一段时间我试图解决。这一点,但没有成功。

I want to say I had progression, because now atleast i get some tables generated in MySQL, but I'm stack on this issue, for a long time i'm trying to solve this, but without success.

我的错误是在这一部分:

The error i get is in this part:

 public void InitializeDatabase(ApplicationDbContext context)
        {
            if (!context.Database.Exists())
            {
                // if database did not exist before - create it
                context.Database.Create();
            }
            else
            {
                // query to check if MigrationHistory table is present in the database 
                var migrationHistoryTableExists = ((IObjectContextAdapter)context).ObjectContext.ExecuteStoreQuery<int>(
                string.Format(
                  "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '{0}' AND table_name = '__MigrationHistory'",
                  "Server=127.0.0.1;Database=mydb;uid=root;pwd=mypass;port=3351;"));

                // if MigrationHistory table is not there (which is the case first time we run) - create it
                if (migrationHistoryTableExists.FirstOrDefault() == 0)
                {
                    context.Database.Delete();
                    context.Database.Create(); //<<<< i get here error
                }
            }
        }



所以这抛出错误: context.Database.Create();

我试图调查究竟是什么问题,但我找不到:(

I tried to investigate what exactly is the problem but i couldn't find :(

我试过比较在SQL Server中生成的数据库中的数据,并把同样的信息在migrationtable,通常我可以把相同的值在那里从工作台INSERT语句。

I tried comparing the data in sql server generated database and put the same info in the migrationtable, and normally i can put the same values in there from insert statement in workbench.

也许有人能帮助我通过这个获得?

Maybe anyone can help me to get through this?

只是要注意,在结束所有表的创建,但没有被插入,因为错误的:/

Just to notice, at the end all of the tables are created but nothing is inserted, because of the error :/

下面是在MySQL的模式中创建的所有表:

Here are all the tables that are created in mysql schema:

我有一个小更新:
看来,它试图让从2列migrationId和contextkey组合PK,这就是为什么你得到这样的错误的。

I have a small update: It seems that it tries to make a combined PK from the 2 columns migrationId and contextkey, that is why you get this kind of error.

我可以重现它在我的工作台我试图改变表,通过设置这些列作为PK的我得到了完全同样的错误。但我怎么说,只有1 PK应该例如设置?

I could reproduce it in my workbench i tried to alter table, by setting those columns as PK's and i got exactly the same error. But how do I say that only 1 PK should be set for example?

推荐答案

此问题是由UTF-8列引起的太长和被用作索引。要解决这一点,最好的方法是使用不同的数据库引擎,但是这可能不是你想听到的东西。第二个选择是更新数据库架构来降低索引的字段字符的限制。

This problem is caused by UTF-8 columns that are too long and are being used as indexes. The best way to solve this would be to use a different database engine, but that is probably not the thing you want to hear. The second choice is to update the database schema to lower the limit of characters in the indexed fields.

http://wildlyinaccurate.com/mysql-specified-key-was-too-long-max-密钥长度为767字节

编辑:
错误在这里doscussed:的 http://bugs.mysql.com/bug.php?id=70940 。最后的后似乎提供了一个可行的slution。

The error is doscussed here: http://bugs.mysql.com/bug.php?id=70940 . The last post seems to offer a viable slution.

这篇关于身份(在数据库创建)错误指定的键太长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆