【问题标题】:ASP.NET-Identity limit UserNameIndex lengthASP.NET-Identity 限制 UserNameIndex 长度
【发布时间】: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


    【解决方案1】:

    简单地说,如果您查看日志(不是那么明显),它会在此查询中崩溃:

    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”表中遇到了同样的错误

    【讨论】:

    • 我一直被这个绊倒,即使我已经修复了很多次,现在至少我有一个参考点,下次我不可避免地会再次破坏它! :)
    • 我也有同样的问题,除了每次手动编辑迁移文件外,有什么解决办法吗?
    【解决方案2】:

    与其在迁移代码中设置最大长度,不如在模型本身上设置。可以使用以下代码完成。

    感谢:Rune G @https://stackoverflow.com/a/29059523/332188

    只需复制以上链接中的代码以方便参考

    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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-03-25
      • 1970-01-01
      • 1970-01-01
      • 2013-10-25
      • 2010-12-06
      • 1970-01-01
      • 2016-08-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多