【问题标题】:EF: Incorrect usage of spatial/fulltext/hash index and explicit index orderEF:空间/全文/哈希索引和显式索引顺序的错误使用
【发布时间】: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


【解决方案1】:

解决了。

在您的迁移文件中,将 .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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-10
    • 2018-10-01
    • 2023-03-29
    • 2018-11-04
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多