Seb*_*uze 6 c# mysql sql entity-framework
我在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
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)
好像在创建索引时使用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; }
}
Run Code Online (Sandbox Code Playgroud)
我将添加索引的DESC HASH在迁移文件中每个.Index()的输出中生成
解决了。
在迁移文件中,将 .Index 条目替换为 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); // REMOVE THIS
Run Code Online (Sandbox Code Playgroud)
在 Up() 方法的底部添加相应的 SQL 命令(对于每个索引)
Sql("CREATE index `IX_userId` on `Articles` (`userId` DESC)");
Run Code Online (Sandbox Code Playgroud)
我随后添加的 DataReaders 问题与 MySQL 连接器相关。MySQL 连接器不支持多个活动连接。要处理这个问题,如果你的控制器中有这个
public IEnumerable<Article> GetArticles()
{
return db.Articles;
}
Run Code Online (Sandbox Code Playgroud)
现在应该是
public IEnumerable<Article> GetArticles()
{
return db.Articles.ToList(); // ToList() will manage the request to work with only ONE data reader,
}
Run Code Online (Sandbox Code Playgroud)
如果您不知道如何将 .Index() 转换为 SQL 命令,只需
update-database -verbose
Run Code Online (Sandbox Code Playgroud)
所有的 SQL 命令都会显示
| 归档时间: |
|
| 查看次数: |
1767 次 |
| 最近记录: |