[英]Specified key was too long; max key length is 767 bytes - ASPNet Identity MySQL
I have created an MVC Application with Identity and MySQL. 我已经使用身份和MySQL创建了MVC应用程序。 I have created my entities but when I come to creating the user table it fails with the error specified in the title.
我已经创建了实体,但是当我创建用户表时,它会失败,并出现标题中指定的错误。 I have searched around and people have said that the
UserName
, Name
and Email
properties are too long. 我到处搜索,有人说
UserName
, Name
和Email
属性太长。 I've tired to set a max length on these columns like the below example but I haven't been able to get it to work. 像下面的示例一样,我已经在这些列上设置了最大长度,但我一直无法使其正常工作。
IdentityModel: IdentityModel:
[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("CaptureDBContext", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUser>().Property(u => u.UserName).IsUnicode(false);
modelBuilder.Entity<ApplicationUser>().Property(u => u.Email).IsUnicode(false);
modelBuilder.Entity<IdentityRole>().Property(r => r.Name).HasMaxLength(255);
}
}
Can anyone help? 有人可以帮忙吗?
I can get it to work with standard entities but not the Identity tables 我可以将其与标准实体一起使用,但不能与身份表一起使用
This question is not a duplicate because the tutorial which is posted in the link is 2 years old, the MySQL.Data version then did not support EF. 这个问题不是重复的,因为链接中发布的教程已有2年的历史了,那时MySQL.Data版本不支持EF。 The one I am using does.
我正在使用的那个。
I found the answer myself. 我自己找到了答案。
The issue was to do with the UserName and Email in the User table. 问题是与用户表中的用户名和电子邮件有关。 And then the Name in the Role table.
然后在角色表中的名称。
I added a max length for all three in my IdentityModel class. 我在IdentityModel类中为这三个添加了最大长度。 Here it is:
这里是:
[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("CaptureDBContext", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUser>().Property(u => u.UserName).HasMaxLength(255);
modelBuilder.Entity<ApplicationUser>().Property(u => u.Email).HasMaxLength(255);
modelBuilder.Entity<IdentityRole>().Property(r => r.Name).HasMaxLength(255);
}
}
}
Oh, and adding the DbConfigurationType
to be MySQL sorts out the migration history table issue(I already knew this). 哦,将
DbConfigurationType
添加为MySQL可以解决迁移历史记录表问题(我已经知道了)。
Unlucky musefan 不幸的缪斯范
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.