通过EntityFramework CodeFirst Migration删除索引 [英] Remove index thru EntityFramework CodeFirst Migration

查看:167
本文介绍了通过EntityFramework CodeFirst Migration删除索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们现在使用EntityFramework CodeFirst Migration。在我们使用EntityFramework CodeFirst Migration之前,我们的数据库表和索引是很久以前创建的。我们的表是myTable,列为vchMyColumnA和索引ColumnA。由于我们使用了EntityFramework CodeFirst
Migration,我们使用EntityFramework CodeFirst Migration向myTable添加新列。

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.

我们需要删除Index ColumnA。

We need to drop Index ColumnA.

1。我们是否需要通过EntityFramework CodeFirst Migration来执行此操作,还是只需运行脚本来删除索引?

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

2。如果我们需要通过EntityFramework CodeFirst Migration删除索引,我该怎么办?在Package Manager Console中,我键入了add-migration DropColumnAIndex,并打开了迁移代码DropColumnAIndex,并添加了以下代码:

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");
    }
}

然后我输入了update-database。没有错误,我可以看到在数据库中迁移(从__MigrationHistor中选择*),但索引ColumnA仍在那里。

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.

谢谢。

推荐答案

嗨aujong,

Hi aujong,

>>然后我输入了update-database。没有错误,我可以请参阅数据库中的迁移(从__MigrationHistor中选择*),但索引ColumnA仍然存在。

根据您的描述和相关代码,我创建一个演示并重现您的问题就在我身边。请修改你的迁移代码,如下所示,它可以在我这边工作,删除数据库的索引。

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


这篇关于通过EntityFramework CodeFirst Migration删除索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆