gpt4 book ai didi

c# - EF : Incorrect usage of spatial/fulltext/hash index and explicit index order

转载 作者:可可西里 更新时间:2023-11-01 07:06:49 25 4
gpt4 key购买 nike

我在我的 WEB Api 项目中使用 Entity Framework 。我使用代码优先迁移。

问题是:在进行初始迁移并尝试更新数据库后,出现此错误

Incorrect usage of spatial/fulltext/hash index and explicit index order

这是由更新数据库中的这条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 HASH

SQL 命令是从迁移中的这段代码生成的:

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 索引创建以便它与简单索引一起使用的任何想法?或者只是简单地绕过这个错误,这样我的更新数据库就可以通过了?谢谢 !

编辑: 添加文章类

public class Article
{
public Article()
{
this.comments = new HashSet<Comment>();
}

[Key]
public int articleId { get; set; }

[Required]
public string title { get; set; }

public string digest { get; set; }

[Required]
public string content { get; set; }

[Required]
public string imgLink { get; set; }

public DateTime? releaseDate { get; set; }

// Clé étrangère (table User)
public int userId { get; set; }

// Auteur de l'article
public virtual User user { get; set; }

// Commentaires
public virtual ICollection<Comment> comments { get; set; }
}

我要补充一点,索引上的 DESC HASH 是在迁移文件中每个 .Index() 的输出中生成的

最佳答案

解决了。

在您的迁移文件中,用如下的 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)");

我随后使用 DataReader 添加的问题与 MySQL 连接器相关。 MySQL 连接器不支持多个事件连接。要处理这个问题,如果你在你的 Controller 中有这个

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命令都会显示

关于c# - EF : Incorrect usage of spatial/fulltext/hash index and explicit index order,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50102420/

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