我刚刚将库Microsoft.AspNet.Identity.EntityFramework更新到最后一个版本(2.0.0.0),我发现创建表时出现了一些错误.当我生成迁移代码(向上和向下方法)时,我无法将更改上传到数据库,因为我在执行“Updata-Database”时遇到索引问题
Specified key was too long; max key length is 767 bytes
@H_404_8@要执行的代码:
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,}) .PrimaryKey(t => t.Id) .Index(t => t.Name,unique: true,name: "RoleNameIndex"); CreateTable( "dbo.AspNetUserRoles",c => new { UserId = c.String(nullable: false,RoleId = c.String(nullable: false,}) .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",Email = c.String(maxLength: 256,EmailConfirmed = c.Boolean(nullable: false),PasswordHash = c.String(maxLength: 256,SecurityStamp = c.String(maxLength: 256,PhoneNumber = c.String(maxLength: 256,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,}) .PrimaryKey(t => t.Id) .Index(t => t.UserName,name: "UserNameIndex"); CreateTable( "dbo.AspNetUserClaims",c => new { Id = c.Int(nullable: false,identity: true),UserId = c.String(nullable: false,ClaimType = c.String(maxLength: 256,ClaimValue = c.String(maxLength: 256,}) .PrimaryKey(t => t.Id) .ForeignKey("dbo.AspNetUsers",cascadeDelete: true) .Index(t => t.UserId); CreateTable( "dbo.AspNetUserLogins",c => new { LoginProvider = c.String(nullable: false,ProviderKey = c.String(nullable: false,}) .PrimaryKey(t => new { t.LoginProvider,t.ProviderKey,t.UserId }) .ForeignKey("dbo.AspNetUsers",cascadeDelete: true) .Index(t => t.UserId); } public override void Down() { DropForeignKey("dbo.AspNetUserRoles","UserId","dbo.AspNetUsers"); DropForeignKey("dbo.AspNetUserLogins","dbo.AspNetUsers"); DropForeignKey("dbo.AspNetUserClaims","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"); } @H_301_15@
当我使用Microsoft.AspNet.Identity.EntityFramework的versión1.0.0.0时,更新数据库的代码是不同的,我没有任何问题
public override void Up() { CreateTable( "dbo.AspNetRoles",}) .PrimaryKey(t => t.Id); CreateTable( "dbo.AspNetUsers",UserName = c.String(maxLength: 256,Discriminator = c.String(nullable: false,}) .PrimaryKey(t => t.Id); CreateTable( "dbo.AspNetUserClaims",User_Id = c.String(nullable: false,t => t.User_Id,cascadeDelete: true) .Index(t => t.User_Id); CreateTable( "dbo.AspNetUserLogins",LoginProvider = c.String(nullable: false,t.LoginProvider,t.ProviderKey }) .ForeignKey("dbo.AspNetUsers",cascadeDelete: true) .Index(t => t.UserId); CreateTable( "dbo.AspNetUserRoles",cascadeDelete: true) .Index(t => t.UserId) .Index(t => t.RoleId); } public override void Down() { DropForeignKey("dbo.AspNetUserRoles","RoleNameIndex"); DropTable("dbo.AspNetUserLogins"); DropTable("dbo.AspNetUserClaims"); DropTable("dbo.AspNetUsers"); DropTable("dbo.AspNetUserRoles"); DropTable("dbo.AspNetRoles"); } @H_301_15@
任何人都可以帮助我尝试解决问题吗?
提前致谢!!
最佳答案
您可能需要查看本教程:http://www.asp.net/mvc/tutorials/security/aspnet-identity-using-mysql-storage-with-an-entityframework-mysql-provider
– 特别是“添加自定义MigrationHistory上下文”部分,该部分解释了如何设置自定义HistoryContext以解决主键大小问题.
– 特别是“添加自定义MigrationHistory上下文”部分,该部分解释了如何设置自定义HistoryContext以解决主键大小问题.