gpt4 book ai didi

asp.net-core - Code First 中的 Identity 和 Mysql - 指定的键太长;最大 key 长度为 1000 字节

转载 作者:行者123 更新时间:2023-12-03 17:34:10 25 4
gpt4 key购买 nike

我正在尝试使用 MySQL 驱动程序创建带有 IentityCore 的 CodeFirst 数据库。

我的 用户 DbModel 是这样的——

[Table("User")]
public class User : IdentityUser<int>
{
public string Name { get; set; }
public string Password { get; set; }
}

角色 DBModel 是这样的——
[Table("Role")]
public class Role : IdentityRole<int>
{
public string Description { get; set; }
}

我的 DbContext 是这样的-
public class InventoryManagementDbContext : IdentityDbContext<User, Role, int>
{
//Table List
public DbSet<Employee> Employies { get; set; }
public DbSet<ProductCategory> ProductCategorys { get; set; }
public DbSet<ProductType> ProductTypes { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<Entry> Entries { get; set; }
public DbSet<Exit> Exits { get; set; }
//Table List End

public InventoryManagementDbContext(DbContextOptions<InventoryManagementDbContext> options)
: base(options)
{}

//Fluent API to make Composite Key
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

// email address doesn't need to be in unicode, check it spec
modelBuilder.Entity<User>().Property(u => u.UserName).IsUnicode(false);
modelBuilder.Entity<User>().Property(u => u.Email).IsUnicode(false);
modelBuilder.Entity<Role>().Property(r => r.Name).HasMaxLength(255);
}
}

因此,我为电子邮件和用户名禁用了 Unicode,并为 设置了 MaxLength角色名称 .

仍然如果我试图执行这个命令 -

Add-Migration



然后我收到此错误-
.....................................
.....................................

fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
Failed executing DbCommand (13ms) [Parameters=[], CommandType='Text', CommandTimeout='60']
CREATE TABLE `AspNetUserLogins` (
`LoginProvider` varchar(767) NOT NULL,
`ProviderKey` varchar(767) NOT NULL,
`ProviderDisplayName` text NULL,
`UserId` int NOT NULL,
PRIMARY KEY (`LoginProvider`, `ProviderKey`),
CONSTRAINT `FK_AspNetUserLogins_AspNetUsers_UserId` FOREIGN KEY (`UserId`) REFERENCES `AspNetUsers` (`Id`) ON DELETE CASCADE
);
MySql.Data.MySqlClient.MySqlException (0x80004005): Specified key was too long; max key length is 1000 bytes
at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId)
at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
at System.Data.Common.DbCommand.ExecuteNonQueryAsync(CancellationToken cancellationToken)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.<ExecuteAsync>d__17.MoveNext()
failverbose: Found DbContext 'InventoryManagementDbContext'.
: Microsoft.EntityFrameworkCore.Database.Command[20102]
Failed executing DbCommand (13ms) [Parameters=[], CommandType='Text', CommandTimeout='60']
CREATE TABLE `AspNetUserLogins` (
`LoginProvider` varchar(767) NOT NULL,
`ProviderKey` varchar(767) NOT NULL,
`ProviderDisplayName` text NULL,
`UserId` int NOT NULL,
PRIMARY KEY (`LoginProvider`, `ProviderKey`),
CONSTRAINT `FK_AspNetUserLogins_AspNetUsers_UserId` FOREIGN KEY (`UserId`) REFERENCES `AspNetUsers` (`Id`) ON DELETE CASCADE
);
MySql.Data.MySqlClient.MySqlException (0x80004005): Specified key was too long; max key length is 1000 bytes
at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId)
at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
at System.Data.Common.DbCommand.ExecuteNonQueryAsync(CancellationToken cancellationToken)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.<ExecuteAsync>d__17.MoveNext()
Microsoft.EntityFrameworkCore.Infrastructure[10403]
Entity Framework Core 2.0.2-rtm-10011 initialized 'InventoryManagementDbContext' using provider 'MySql.Data.EntityFrameworkCore' with options: NoTracking CommandTimeout=60
Microsoft.EntityFrameworkCore.Infrastructure[10403]
Entity Framework Core 2.0.2-rtm-10011 initialized 'InventoryManagementDbContext' using provider 'MySql.Data.EntityFrameworkCore' with options: NoTracking CommandTimeout=60

我的完整代码可以在 中找到here .

有人可以帮忙吗?

关于-

This question没有解决我的问题,因为我没有遇到 General Keys 的任何问题,它们非常适合我。

我在尝试使用时遇到问题 Identity .

最佳答案

对于任何有同样问题的人,即使问题已经有一段时间了,我也会把它留在这里。

您需要为以下几个属性设置最大长度:

  • 身份角色
  • 身份用户
  • 身份用户登录
  • IdentityUserToken

  • 在本例中,您扩展了类 IdentityUserIdentityRole使用 integer PK,所以你的代码,使用 fluent API,应该是这样的:

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
    base.OnModelCreating(modelBuilder);

    int stringMaxLength = /* something like 100*/;
    // User IdentityRole and IdentityUser in case you haven't extended those classes
    builder.Entity<Role>(x => x.Property(m => m.Name).HasMaxLength(stringMaxLength));
    builder.Entity<Role>(x => x.Property(m => m.NormalizedName).HasMaxLength(stringMaxLength));
    builder.Entity<User>(x => x.Property(m => m.NormalizedUserName).HasMaxLength(stringMaxLength));

    // We are using int here because of the change on the PK
    builder.Entity<IdentityUserLogin<int>>(x => x.Property(m => m.LoginProvider).HasMaxLength(stringMaxLength));
    builder.Entity<IdentityUserLogin<int>>(x => x.Property(m => m.ProviderKey).HasMaxLength(stringMaxLength));

    // We are using int here because of the change on the PK
    builder.Entity<IdentityUserToken<int>>(x => x.Property(m => m.LoginProvider).HasMaxLength(stringMaxLength));
    builder.Entity<IdentityUserToken<int>>(x => x.Property(m => m.Name).HasMaxLength(stringMaxLength));

    // etc..

    希望能帮助到你!

    关于asp.net-core - Code First 中的 Identity 和 Mysql - 指定的键太长;最大 key 长度为 1000 字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49573740/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com