EF: Incorrect usage of spatial/fulltext/hash index and explicit index order
我在我的WEB Api项目中使用了Entity框架。
我使用代码优先迁移。
问题是:进行初始迁移并尝试更新数据库后,出现此错误
Incorrect usage of spatial/fulltext/hash index and explicit index order
这是由更新数据库中的以下SQL命令引起的:
1 2 3 4 5 6 7 8 9 10 11 12 | 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命令是从迁移中的以下代码生成的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | 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; } } |
我将添加在迁移文件中每个.Index()的输出中生成的索引上的DESC HASH
解决了。
在您的迁移文件中,用如下的sql命令替换.Index条目
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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命令(对于每个索引)
1 | Sql("CREATE index `IX_userId` on `Articles` (`userId` DESC)"); |
我随后在DataReaders中添加的问题与MySQL连接器有关。 MySQL连接器不支持多个活动连接。要处理此问题,如果您的控制器中有此命令
1 2 3 4 | public IEnumerable<Article> GetArticles() { return db.Articles; } |
现在应该是
1 2 3 4 | public IEnumerable<Article> GetArticles() { return db.Articles.ToList(); // ToList() will manage the request to work with only ONE data reader, } |
如果您不知道如何将.Index()转换为SQL命令,只需
1 | update-database -verbose |
,所有SQL命令将显示