加入收藏 | 设为首页 | 会员中心 | 我要投稿 辽源站长网 (https://www.0437zz.com/)- 云专线、云连接、智能数据、边缘计算、数据安全!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

c# – 身份实体框架库 – 更新数据库[MySQL]

发布时间:2021-03-14 23:04:09 所属栏目:MySql教程 来源:网络整理
导读:我刚刚将库Microsoft.AspNet.Identity.EntityFramework更新到最后一个版本(2.0.0.0),我发现创建表时出现了一些错误.当我生成迁移代码(向上和向下方法)时,我无法将更改上传到数据库,因为我在执行“Updata-Database”时遇到索引问题 Specified key was too lon

我刚刚将库Microsoft.AspNet.Identity.EntityFramework更新到最后一个版本(2.0.0.0),我发现创建表时出现了一些错误.当我生成迁移代码(向上和向下方法)时,我无法将更改上传到数据库,因为我在执行“Updata-Database”时遇到索引问题

Specified key was too long; max key length is 767 bytes

要执行的代码:

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

当我使用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");
    }

任何人都可以帮助我尝试解决问题吗?

提前致谢!!

最佳答案 您可能需要查看本教程:http://www.asp.net/mvc/tutorials/security/aspnet-identity-using-mysql-storage-with-an-entityframework-mysql-provider
? – 特别是“添加自定义MigrationHistory上下文”部分,该部分解释了如何设置自定义HistoryContext以解决主键大小问题.

(编辑:辽源站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读