【发布时间】:2018-10-10 15:52:30
【问题描述】:
我在我的 WEB Api 项目中使用实体框架。 我使用代码优先迁移。
问题是:在进行初始迁移并尝试更新数据库后,我收到此错误
空间/全文/哈希索引和显式索引顺序的错误使用
这是由更新数据库中的这条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() 的输出中生成的
【问题讨论】:
-
您好,如果您使用的是数据注释;你能显示实体文章吗?
-
@Stefan 刚刚添加了它
-
嗯...太糟糕了...看起来不错
-
关于为什么以这种方式生成索引的任何想法?我试图从迁移文件中删除索引,但显然我无法获取我的文章
-
我尝试在迁移到原始 SQL 时替换 .Index(仅删除 HASH,其余 cmd 相同)。没有更多的哈希索引错误,但是我的 Get 控制器不起作用..(无法收集 cmets,说 DataReader 已经打开,需要先关闭)Feelsbad
标签: c# mysql sql entity-framework