如何删除现有迁移并从头开始添加?

问题描述

我刚刚使用以下模型创建了迁移,并更新了数据库并创建了表

public class ZipSearchHistory: BaseEntity
{
    public string City { get; set; }
    public string State { get; set; }
    public string ZipCode { get; set; }
    public int Count { get; set; }
}

我意识到ZipCode中将使用int而不是string。因此,为了从头开始,我删除了所有迁移文件,并手动删除了SSMS中的表。现在,我应用了迁移。

我的迁移代码是:

public partial class ZipCodeSearchHistoryTable : DbMigration
{
    public override void Up()
    {
        AddColumn("dbo.ZipSearchHistories","ZipCode",c => c.Int(nullable: false));
        DropColumn("dbo.ZipSearchHistories","Zip");
    }
    
    public override void Down()
    {
        AddColumn("dbo.ZipSearchHistories","Zip",c => c.String());
        DropColumn("dbo.ZipSearchHistories","ZipCode");
    }
}

当我尝试操作更新数据库时,我得到一个错误

找不到对象“ dbo.ZipSearchHistories”,因为该对象不存在或您没有权限

我该如何解决?

解决方法

如果您再次从 scratch 开始,则还删除了迁移文件,我会说也删除dbcontext快照文件,因为似乎您没有。

因为现在发生的是实体框架将更改与快照文件进行了比较。 (这就是为什么您只看到对zipcode列所做的更改,而看不到它再次创建一个新的新表的原因,正如您期望的那样,因为您从db中的迁移表中删除了数据)>

https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/migrations?view=aspnetcore-3.1

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...