指定的密钥太长;最大密钥长度为767字节-ASPNet Identity MySQL [英] Specified key was too long; max key length is 767 bytes - ASPNet Identity MySQL
问题描述
我用Identity和MySQL创建了一个MVC应用程序.我已经创建了实体,但是当我创建用户表时,它会失败,并出现标题中指定的错误.
我到处搜索,人们说UserName
,Name
和Email
属性太长.我已经厌倦了像下面的示例那样在这些列上设置最大长度,但是我无法使其正常工作.
I have created an MVC Application with Identity and MySQL. 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. 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?
我可以使它与标准实体一起使用,但不能与Identity表一起使用
I can get it to work with standard entities but not the Identity tables
这个问题不是重复的,因为链接中发布的教程已有2年的历史了,那时MySQL.Data版本不支持EF.我正在使用的那个.
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. The one I am using does.
推荐答案
我自己找到了答案.
问题与用户表中的用户名和电子邮件有关.然后在角色表中的名称.
The issue was to do with the UserName and Email in the User table. And then the Name in the Role table.
我在IdentityModel类中为所有三个添加了最大长度.在这里:
I added a max length for all three in my IdentityModel class. 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);
}
}
}
哦,并添加DbConfigurationType
为MySQL可以解决迁移历史记录表问题(我已经知道了).
Oh, and adding the DbConfigurationType
to be MySQL sorts out the migration history table issue(I already knew this).
倒霉的博物馆迷
这篇关于指定的密钥太长;最大密钥长度为767字节-ASPNet Identity MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!