为了账号安全,请及时绑定邮箱和手机立即绑定

EF:不正确使用空间/全文/哈希索引和显式索引顺序

EF:不正确使用空间/全文/哈希索引和显式索引顺序

C#
慕的地6264312 2021-05-07 14:16:48
我在我的WEB Api项目中使用了Entity Framework。我使用代码优先迁移。事情是:进行初始迁移并尝试更新数据库后,出现此错误不正确使用空间/全文/哈希索引和显式索引顺序这是由更新数据库中的此SQL命令引起的:create table `Articles` ( `articleId` int not null  auto_increment , `title` longtext not null , `digest` longtext, `content` longtext not null , `imgLink` longtext not null , `releaseDate` datetime, `userId` int not null ,  primary key ( `articleId`)) engine=InnoDb auto_increment=0  CREATE index  `IX_userId` on `Articles` (`userId` DESC) using HASHSQL命令是从迁移中的以下代码生成的:CreateTable(            "dbo.Articles",            c => new                {                    articleId = c.Int(nullable: false, identity: true),                    title = c.String(nullable: false, unicode: false),                    digest = c.String(unicode: false),                    content = c.String(nullable: false, unicode: false),                    imgLink = c.String(nullable: false, unicode: false),                    releaseDate = c.DateTime(precision: 0),                    userId = c.Int(nullable: false),                })            .PrimaryKey(t => t.articleId)            .ForeignKey("dbo.Users", t => t.userId, cascadeDelete: true)            .Index(t => t.userId);似乎在创建索引时出现DESC和HASH会导致此错误。关于如何更改生成的sql索引创建的任何想法,使其与简单的索引一起使用?或者只是简单地绕过此错误,以便我的更新数据库可以通过?谢谢 !
查看完整描述

1 回答

?
阿晨1998

TA贡献2037条经验 获得超6个赞

解决了。


在您的迁移文件中,用如下的sql命令替换.Index条目


CreateTable(

        "dbo.Articles",

        c => new

            {

                articleId = c.Int(nullable: false, identity: true),

                title = c.String(nullable: false, unicode: false),

                digest = c.String(unicode: false),

                content = c.String(nullable: false, unicode: false),

                imgLink = c.String(nullable: false, unicode: false),

                releaseDate = c.DateTime(precision: 0),

                userId = c.Int(nullable: false),

            })

        .PrimaryKey(t => t.articleId)

        .ForeignKey("dbo.Users", t => t.userId, cascadeDelete: true)

        .Index(t => t.userId); // REMOVE THIS

在Up()方法的底部添加相应的SQL命令(对于每个索引)


Sql("CREATE index `IX_userId` on `Articles` (`userId` DESC)");

然后我为DataReaders添加的问题与MySQL连接器有关。MySQL连接器不支持多个活动连接。要处理此问题,如果您的控制器中有此命令


public IEnumerable<Article> GetArticles()

{

    return db.Articles;

}

现在应该是


public IEnumerable<Article> GetArticles()

{

    return db.Articles.ToList(); // ToList() will manage the request to work with only ONE data reader, 

}

如果您不知道如何将.Index()转换为SQL命令,只需


update-database -verbose

并且所有的SQL命令将显示


查看完整回答
反对 回复 2021-05-21
  • 1 回答
  • 0 关注
  • 176 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信