Identity (on database creation) error specified ke

2019-04-13 14:38发布

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:

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

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: mysql columns

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?

3条回答
Explosion°爆炸
2楼-- · 2019-04-13 15:06

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.

查看更多
甜甜的少女心
3楼-- · 2019-04-13 15:06

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:

public class ApplicationUser : IdentityUser
{
  [StringLength(200)]
  public override string UserName { get; set; }   
}
查看更多
啃猪蹄的小仙女
4楼-- · 2019-04-13 15:23

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.

    public class WebPortalDbContext : IdentityDbContext<ApplicationUser>
{
    public WebPortalDbContext()
        : base("IdentityConnection")
    {

    }

    public static WebPortalDbContext Create()
    {
        return new WebPortalDbContext();
    }

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

        modelBuilder.Entity<Microsoft.AspNet.Identity.EntityFramework.IdentityRole>()
            .Property(c => c.Name).HasMaxLength(128).IsRequired();

        modelBuilder.Entity<Microsoft.AspNet.Identity.EntityFramework.IdentityUser>().ToTable("AspNetUsers")//I have to declare the table name, otherwise IdentityUser will be created
            .Property(c => c.UserName).HasMaxLength(128).IsRequired();
    }


}

To clarify the solution, here's the libraries I am using:

  1. EF 6.1.0
  2. Microsoft ASP.NET Identity EntityFramework 2.0.0
  3. ASP.NET Identity Owin 2.0.0
  4. MySql .NET libraries 6.8.3

And this solution also works for

  1. EF 6.1.1
  2. Microsoft ASP.NET Identity EntityFramework 2.0.1
  3. ASP.NET Identity Owin 2.0.1
查看更多
登录 后发表回答