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

前端 未结 1 1424
遇见更好的自我
遇见更好的自我 2021-01-12 15:01

I\'m using Entity framework with my WEB Api project. I use code first migration.

The thing is: After making my intitial migration and trying to update database, I g

相关标签:
1条回答
  • 2021-01-12 15:08

    Solved it.

    In your migration file, replace .Index entries by sql commands like below

    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
    

    Add the corresponding SQL command at the bottom of your Up() method (for every index)

    Sql("CREATE index `IX_userId` on `Articles` (`userId` DESC)");
    

    The problems I add then with DataReaders are MySQL connector related. MySQL connector doesn't support multiple active connections. To handle this, if you had this in your controller

    public IEnumerable<Article> GetArticles()
    {
        return db.Articles;
    }
    

    Now it should be

    public IEnumerable<Article> GetArticles()
    {
        return db.Articles.ToList(); // ToList() will manage the request to work with only ONE data reader, 
    }
    

    If you don't know how to convert your .Index() to SQL commands, just

    update-database -verbose
    

    and all the SQL commands will show

    0 讨论(0)
提交回复
热议问题