如何限制表AspNetUsers中的UserNameIndex UNIQUE索引?
我正在使用带有Mysql后端的ASP.NET身份,并且正在运行另一个实例:
Index column size too large. The maximum column size is 767 bytes.
我试过了
modelBuilder.Entity<Secuser>().Property(x => x.UserName).HasMaxLength(100);
modelBuilder.Entity<Secuser>().Property(t => t.Id).HasMaxLength(100);
我已经完成了所有标准:
public class Secuser : IdentityUser
{
[Required]
public string FullName { get; set; }
[Required]
public string Address { get; set; }
[Required]
public string City { get; set; }
[Required]
public string Country { get; set; }
[Required]
public bool AgreeTermsOfServicePrivacyPolicy { get; set; }
[Required]
public string BasePath { get; set; }
}
[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class ApplicationDbContext : IdentityDbContext<Secuser>
{
public ApplicationDbContext() : base("DefaultConnection")
{
}
.....
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Secuser>().Property(x => x.UserName).HasMaxLength(100);
modelBuilder.Entity<Secuser>().Property(t => t.Id).HasMaxLength(100);
.....
base.OnModelCreating(modelBuilder);
}
}
当我发出:
update-database -verbose
输出是:
Using StartUp project 'sec'.
Using NuGet project 'sec'.
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Target database is: 'sec' (DataSource: localhost, Provider: MySql.Data.MySqlClient, Origin: Configuration).
Applying explicit migrations: [201405072209015_InitialCreate].
Applying explicit migration: 201405072209015_InitialCreate.
.....
create table `AspNetUsers` (`Id` nvarchar(128) not null ,`FullName` longtext not null ,`Address` longtext not null ,`City` longtext not null ,`Country` longtext not null ,`AgreeTermsOfServicePrivacyPolicy` bool not null ,`BasePath` longtext not null ,`Email` nvarchar(256) ,`EmailConfirmed` bool not null ,`PasswordHash` longtext,`SecurityStamp` longtext,`PhoneNumber` longtext,`PhoneNumberConfirmed` bool not null ,`TwoFactorEnabled` bool not null ,`LockoutEndDateUtc` datetime,`LockoutEnabled` bool not null ,`AccessFailedCount` int not null ,`UserName` nvarchar(256) not null ,primary key ( `Id`) ) engine=InnoDb auto_increment=0
CREATE UNIQUE index `UserNameIndex` on `AspNetUsers` (`UserName` DESC) using HASH
MySql.Data.MySqlClient.MySqlException (0x80004005): Index column size too large. The maximum column size is 767 bytes.
at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId)
.....
我还进行了更改以允许迁移历史记录工作:
public class MySqlHistoryContext : HistoryContext
{
public MySqlHistoryContext(DbConnection connection, string defaultSchema):base(connection,defaultSchema)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<HistoryRow>().Property(h => h.MigrationId).HasMaxLength(100).IsRequired();
modelBuilder.Entity<HistoryRow>().Property(h => h.ContextKey).HasMaxLength(200).IsRequired();
}
}
似乎整个问题都围绕着
`UserName` nvarchar(256)
modelBuilder.Entity<Secuser>().Property(x => x.UserName).HasMaxLength(100);
似乎无法正常工作。我正在使用EntityFramework 6.1.0和Microsoft.AspNet.Identity.Core 2.0.1。
所以,看起来似乎是:
ASP.NET-Identity limit UserName length
要么我错过了我实施HasMaxLength的方法,要么就是为什么它不起作用。
答案 0 :(得分:2)
简单地说,如果您查看日志(不是那么明显),它会在此查询中崩溃:
CREATE UNIQUE index `UserNameIndex` on `AspNetUsers` (`UserName` DESC) using HASH
当您查看迁移代码,up()函数时,默认情况下会看到:
UserName = c.String(nullable: false, maxLength: 256, storeType: "nvarchar"),
我刚刚将maxLength值更改为196它应该工作。你只需要记住,用户名不能超过196个字符(我认为这对于99.9%的人口来说已经足够了)
PS:我在“AspNetRoles”表中得到了同样的错误
答案 1 :(得分:1)
不是在迁移代码中设置最大长度,而是在模型本身上设置它更好。可以使用以下代码完成。
感谢:符文G @ https://stackoverflow.com/a/29059523/332188&lt; - 在这里解释
只需复制上述链接中的代码即可轻松参考
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
//... default code for ApplicationDbContext
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
if (modelBuilder == null)
{
throw new ArgumentNullException("modelBuilder");
}
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>().Property(u => u.UserName).HasMaxLength(128);
//Uncomment this to have Email length 128 too (not neccessary)
//modelBuilder.Entity<ApplicationUser>().Property(u => u.Email).HasMaxLength(128);
modelBuilder.Entity<IdentityRole>().Property(r => r.Name).HasMaxLength(128);
}
}