[英]Identity Entity Framework Library - Update Database [MySQL]
我刚刚将库Microsoft.AspNet.Identity.EntityFramework更新到最后一个版本(2.0.0.0),我发现创建表时出现了一些错误。 当我生成迁移代码(向上和向下方法)时,我无法将更改上传到数据库,因为我在执行“Updata-Database”时遇到索引问题
指定密钥太长; 最大密钥长度为767字节
要执行的代码:
public override void Up()
{
CreateTable(
"dbo.AspNetRoles",
c => new
{
Id = c.String(nullable: false, maxLength: 128, storeType: "nvarchar"),
Name = c.String(nullable: false, maxLength: 256, storeType: "nvarchar"),
})
.PrimaryKey(t => t.Id)
.Index(t => t.Name, unique: true, name: "RoleNameIndex");
CreateTable(
"dbo.AspNetUserRoles",
c => new
{
UserId = c.String(nullable: false, maxLength: 128, storeType: "nvarchar"),
RoleId = c.String(nullable: false, maxLength: 128, storeType: "nvarchar"),
})
.PrimaryKey(t => new { t.UserId, t.RoleId })
.ForeignKey("dbo.AspNetRoles", t => t.RoleId, cascadeDelete: true)
.ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
.Index(t => t.UserId)
.Index(t => t.RoleId);
CreateTable(
"dbo.AspNetUsers",
c => new
{
Id = c.String(nullable: false, maxLength: 128, storeType: "nvarchar"),
Email = c.String(maxLength: 256, storeType: "nvarchar"),
EmailConfirmed = c.Boolean(nullable: false),
PasswordHash = c.String(maxLength: 256, storeType: "nvarchar"),
SecurityStamp = c.String(maxLength: 256, storeType: "nvarchar"),
PhoneNumber = c.String(maxLength: 256, storeType: "nvarchar"),
PhoneNumberConfirmed = c.Boolean(nullable: false),
TwoFactorEnabled = c.Boolean(nullable: false),
LockoutEndDateUtc = c.DateTime(precision: 0),
LockoutEnabled = c.Boolean(nullable: false),
AccessFailedCount = c.Int(nullable: false),
UserName = c.String(nullable: false, maxLength: 256, storeType: "nvarchar"),
})
.PrimaryKey(t => t.Id)
.Index(t => t.UserName, unique: true, name: "UserNameIndex");
CreateTable(
"dbo.AspNetUserClaims",
c => new
{
Id = c.Int(nullable: false, identity: true),
UserId = c.String(nullable: false, maxLength: 128, storeType: "nvarchar"),
ClaimType = c.String(maxLength: 256, storeType: "nvarchar"),
ClaimValue = c.String(maxLength: 256, storeType: "nvarchar"),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
.Index(t => t.UserId);
CreateTable(
"dbo.AspNetUserLogins",
c => new
{
LoginProvider = c.String(nullable: false, maxLength: 128, storeType: "nvarchar"),
ProviderKey = c.String(nullable: false, maxLength: 128, storeType: "nvarchar"),
UserId = c.String(nullable: false, maxLength: 128, storeType: "nvarchar"),
})
.PrimaryKey(t => new { t.LoginProvider, t.ProviderKey, t.UserId })
.ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
.Index(t => t.UserId);
}
public override void Down()
{
DropForeignKey("dbo.AspNetUserRoles", "UserId", "dbo.AspNetUsers");
DropForeignKey("dbo.AspNetUserLogins", "UserId", "dbo.AspNetUsers");
DropForeignKey("dbo.AspNetUserClaims", "UserId", "dbo.AspNetUsers");
DropForeignKey("dbo.AspNetUserRoles", "RoleId", "dbo.AspNetRoles");
DropIndex("dbo.AspNetUserLogins", new[] { "UserId" });
DropIndex("dbo.AspNetUserClaims", new[] { "UserId" });
DropIndex("dbo.AspNetUsers", "UserNameIndex");
DropIndex("dbo.AspNetUserRoles", new[] { "RoleId" });
DropIndex("dbo.AspNetUserRoles", new[] { "UserId" });
DropIndex("dbo.AspNetRoles", "RoleNameIndex");
DropTable("dbo.AspNetUserLogins");
DropTable("dbo.AspNetUserClaims");
DropTable("dbo.AspNetUsers");
DropTable("dbo.AspNetUserRoles");
DropTable("dbo.AspNetRoles");
}
当我使用Microsoft.AspNet.Identity.EntityFramework的versión1.0.0.0时,更新数据库的代码是不同的,我没有任何问题
public override void Up()
{
CreateTable(
"dbo.AspNetRoles",
c => new
{
Id = c.String(nullable: false, maxLength: 128, storeType: "nvarchar"),
Name = c.String(nullable: false, maxLength: 256, storeType: "nvarchar"),
})
.PrimaryKey(t => t.Id);
CreateTable(
"dbo.AspNetUsers",
c => new
{
Id = c.String(nullable: false, maxLength: 128, storeType: "nvarchar"),
UserName = c.String(maxLength: 256, storeType: "nvarchar"),
PasswordHash = c.String(maxLength: 256, storeType: "nvarchar"),
SecurityStamp = c.String(maxLength: 256, storeType: "nvarchar"),
Discriminator = c.String(nullable: false, maxLength: 128, storeType: "nvarchar"),
})
.PrimaryKey(t => t.Id);
CreateTable(
"dbo.AspNetUserClaims",
c => new
{
Id = c.Int(nullable: false, identity: true),
ClaimType = c.String(maxLength: 256, storeType: "nvarchar"),
ClaimValue = c.String(maxLength: 256, storeType: "nvarchar"),
User_Id = c.String(nullable: false, maxLength: 128, storeType: "nvarchar"),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.AspNetUsers", t => t.User_Id, cascadeDelete: true)
.Index(t => t.User_Id);
CreateTable(
"dbo.AspNetUserLogins",
c => new
{
UserId = c.String(nullable: false, maxLength: 128, storeType: "nvarchar"),
LoginProvider = c.String(nullable: false, maxLength: 128, storeType: "nvarchar"),
ProviderKey = c.String(nullable: false, maxLength: 128, storeType: "nvarchar"),
})
.PrimaryKey(t => new { t.UserId, t.LoginProvider, t.ProviderKey })
.ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
.Index(t => t.UserId);
CreateTable(
"dbo.AspNetUserRoles",
c => new
{
UserId = c.String(nullable: false, maxLength: 128, storeType: "nvarchar"),
RoleId = c.String(nullable: false, maxLength: 128, storeType: "nvarchar"),
})
.PrimaryKey(t => new { t.UserId, t.RoleId })
.ForeignKey("dbo.AspNetRoles", t => t.RoleId, cascadeDelete: true)
.ForeignKey("dbo.AspNetUsers", t => t.UserId, cascadeDelete: true)
.Index(t => t.UserId)
.Index(t => t.RoleId);
}
public override void Down()
{
DropForeignKey("dbo.AspNetUserRoles", "UserId", "dbo.AspNetUsers");
DropForeignKey("dbo.AspNetUserLogins", "UserId", "dbo.AspNetUsers");
DropForeignKey("dbo.AspNetUserClaims", "UserId", "dbo.AspNetUsers");
DropForeignKey("dbo.AspNetUserRoles", "RoleId", "dbo.AspNetRoles");
DropIndex("dbo.AspNetUserLogins", new[] { "UserId" });
DropIndex("dbo.AspNetUserClaims", new[] { "UserId" });
DropIndex("dbo.AspNetUsers", "UserNameIndex");
DropIndex("dbo.AspNetUserRoles", new[] { "RoleId" });
DropIndex("dbo.AspNetUserRoles", new[] { "UserId" });
DropIndex("dbo.AspNetRoles", "RoleNameIndex");
DropTable("dbo.AspNetUserLogins");
DropTable("dbo.AspNetUserClaims");
DropTable("dbo.AspNetUsers");
DropTable("dbo.AspNetUserRoles");
DropTable("dbo.AspNetRoles");
}
任何人都可以帮助我尝试解决问题吗?
提前致谢!!
我知道这是一个老帖子,但我今天遇到了同样的问题并对此进行了一些调查,并希望分享我的发现和解决方案。
问题是Microsoft在版本之间为AspNetRoles
的Name
列添加了一个唯一索引,并且由于此列的大小为256,因此它破坏了MySql的索引规则。 AspNetUsers
上的Name
列上也会出现此问题。
所以,我去分析如何解决这个问题,我认为最正确的做法是减少Name
列的长度(恕我直言,实际上没有任何理由让一个角色/用户的名字很长)。
在调查IdentityDbContext(这是基类)上的代码后,我认为解决此问题的最正确的方法是覆盖ApplicationDbContext
的OnModelCreating
并调整列大小,如下所示:
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<ApplicationUser>().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);
}
}
此代码将使用base.OnModelCreating
调用初始化具有默认配置的标识模型,然后使用我们自己的调用覆盖基类中完成的设置。
由于您要创建迁移项目,因此您需要重新运行迁移创建以获取更新的模型(或手动修改模型以调整列大小)。
这将解决问题,您将拥有Asp.NET Identity系统的全部功能。
当然,为了不在创建项目时出现任何错误,您只需修改您的迁移代码,但随后您将与您的上下文定义的模型有所不同,这可能会导致问题。
这可能是由于您的数据库使用UTF8
排序规则。
解决此问题的一种可能方法是将包含maxLength: 256
的代码的所有部分更改为maxLength: 190
。
另一种方法是将数据库上的UTF8
排序规则更改为latin1
或类似的排序规则。
您可能需要查看本教程: http : //www.asp.net/mvc/tutorials/security/aspnet-identity-using-mysql-storage-with-an-entityframework-mysql-provider - 特别是“添加”部分自定义MigrationHistory上下文“,它解释了如何设置自定义HistoryContext以解决主键大小问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.