none
Remove index thru EntityFramework CodeFirst Migration RRS feed

  • Question

  • We now use EntityFramework CodeFirst Migration. Our database table and index was created long time ago, before we use EntityFramework CodeFirst Migration. Our table is myTable, with a column vchMyColumnA and index ColumnA. Since we used EntityFramework CodeFirst Migration, we have used EntityFramework CodeFirst Migration to add new columns to myTable.

    We need to drop Index ColumnA.

    1. Do we need to do it thru EntityFramework CodeFirst Migration, or can we just run a script to drop the index ?

    2. If we need to drop the index thru EntityFramework CodeFirst Migration, how can I do it ? In Package Manager Console, I typed add-migration DropColumnAIndex, and open the migration code DropColumnAIndex, and added this code:

        public partial class DropColumnAIndex : DbMigration
    {
        public override void Up()
        {
            DropIndex("dbo.myTable", new[] { "ColumnA" });
        }
    
        public override void Down()
        {
            CreateIndex("dbo.myTable", "vchMyColumnA");
        }
    }

    Then I typed update-database. There is no error, and I can see the migration in the database (select * from __MigrationHistor), but index ColumnA is still there.

    Thank you.


    • Edited by aujong Monday, May 15, 2017 4:42 PM
    Monday, May 15, 2017 4:18 PM

All replies

  • Hi aujong,

    >>Then I typed update-database. There is no error, and I can see the migration in the database (select * from __MigrationHistor), but index ColumnA is still there.

    Based on your description and related code, I create a demo and reproduce your issue on my side. please modify your migration code like below, it works on my side, which remove the index of database.

    public override void Up()
    {
        DropIndex("dbo.myTable", "ColumnA");
    }
            
    public override void Down()
    {
        CreateIndex("dbo.myTable", "vchMyColumnA");
    }

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 19, 2017 2:12 AM
    Moderator