【发布时间】:2014-06-28 06:16:18
【问题描述】:
如何限制 AspNetUsers 表中的 UserNameIndex UNIQUE 索引?
我正在使用带有 Mysql 后端的 ASP.NET Identity,并且正在运行以下实例:
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 的过程中遗漏了一些东西,要么有其他原因导致它不起作用。
【问题讨论】:
标签: mysql entity-framework ef-code-first entity-framework-6 asp.net-identity