Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql ASP.NET-Identity限制用户名索引长度_Mysql_Entity Framework_Ef Code First_Entity Framework 6_Asp.net Identity - Fatal编程技术网

Mysql ASP.NET-Identity限制用户名索引长度

Mysql ASP.NET-Identity限制用户名索引长度,mysql,entity-framework,ef-code-first,entity-framework-6,asp.net-identity,Mysql,Entity Framework,Ef Code First,Entity Framework 6,Asp.net Identity,如何限制表AspNetUsers中的UserNameIndex唯一索引 我将ASP.NET Identity与Mysql后端一起使用,并遇到另一个实例: Index column size too large. The maximum column size is 767 bytes. 我试过了 modelBuilder.Entity<Secuser>().Property(x => x.UserName).HasMaxLength(100); modelBuilder.En

如何限制表AspNetUsers中的UserNameIndex唯一索引

我将ASP.NET Identity与Mysql后端一起使用,并遇到另一个实例:

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);
输出为:

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();
  }
 }
公共类MySqlHistoryContext:HistoryContext
{
public MySqlHistoryContext(DbConnection,string defaultSchema):base(connection,defaultSchema)
{
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
基于模型创建(modelBuilder);
modelBuilder.Entity().Property(h=>h.MigrationId).HasMaxLength(100).IsRequired();
modelBuilder.Entity().Property(h=>h.ContextKey).HasMaxLength(200).IsRequired();
}
}
看来整个问题都围绕着

`UserName` nvarchar(256)

modelBuilder.Entity<Secuser>().Property(x => x.UserName).HasMaxLength(100);
`UserName`nvarchar(256)
modelBuilder.Entity().Property(x=>x.UserName).HasMaxLength(100);
似乎不起作用。我正在使用EntityFramework 6.1.0和Microsoft.AspNet.Identity.Core 2.0.1

因此,从以下几个方面来看:


要么我在实现HasMaxLength的过程中遗漏了一些东西,要么是因为其他原因导致它无法工作。

简单地说,如果您查看日志(不是很明显),它会在这个查询上崩溃:

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”表,我得到了相同的错误,与其在迁移代码中设置最大长度,不如在模型本身上设置它。可以使用以下代码来完成

感谢:符文G@u.UserName); //取消对此的注释,使电子邮件长度也达到128(不是必需的) //modelBuilder.Entity().Property(u=>u.Email).HasMaxLength(128); modelBuilder.Entity().Property(r=>r.Name).HasMaxLength(128); } }
我一直被这个问题绊倒,即使我已经修复了很多次,现在至少我有了一个参考点,下次我不可避免地再次破坏东西时!)我也有同样的问题,除了每次手工编辑迁移文件外,还有什么解决办法吗?
CREATE UNIQUE index  `UserNameIndex` on `AspNetUsers` (`UserName` DESC) using HASH
 UserName = c.String(nullable: false, maxLength: 256, storeType: "nvarchar"),
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);
    }
}