I have here an issue with AspNet Identity and MySql, the issue is error which is really blocking me, this error is following:
specified key was too long max key length is 767 bytes
Now I followed this tutorial of using MySql with Identity:
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
}
}
}
So this throws error : context.Database.Create();
I tried to investigate what exactly is the problem but i couldn't find :(
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 :/
Here are all the tables that are created in mysql schema:
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.
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?
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-key-length-is-767-bytes
EDIT: The error is doscussed here: http://bugs.mysql.com/bug.php?id=70940 . The last post seems to offer a viable slution.
Just a shot in the dark ... I think the column that's being created for Application.UserName is too large for a unique index. See if you can influence the size of it by overriding the default:
Through checking the SQL queries generated by EF, I had found that the problem was related to UserName (varchar utf8 256) in table AspNetUsers and Name (varchar utf8 256) in table AspNetRoles, while the table for HistoryRow was fine.
So the following codes resolved the problem.
To clarify the solution, here's the libraries I am using:
And this solution also works for