我最近更改了类中的一些属性(通知),并且像往常一样,我为更改添加了迁移。 我运行 update-database 命令并收到错误消息,指出表“xxx.dbo.notifications”不存在。
表格仍然存在,为什么我会收到此错误?
编辑:迁移代码
public override void Up()
{
DropForeignKey("dbo.Notifications", "PaymentId", "dbo.Payments");
DropIndex("dbo.Notifications", new[] { "PaymentId" });
AddColumn("dbo.Notifications", "ProductOptionId", c => c.Int());
AlterColumn("dbo.Notifications", "PaymentId", c => c.Int());
CreateIndex("dbo.Notifications", "PaymentId");
CreateIndex("dbo.Notifications", "ProductOptionId");
AddForeignKey("dbo.Notifications", "ProductOptionId", "dbo.ProductOptions", "Id");
AddForeignKey("dbo.Notifications", "PaymentId", "dbo.Payments", "Id");
}
public override void Down()
{
DropForeignKey("dbo.Notifications", "PaymentId", "dbo.Payments");
DropForeignKey("dbo.Notifications", "ProductOptionId", "dbo.ProductOptions");
DropIndex("dbo.Notifications", new[] { "ProductOptionId" });
DropIndex("dbo.Notifications", new[] { "PaymentId" });
AlterColumn("dbo.Notifications", "PaymentId", c => c.Int(nullable: false));
DropColumn("dbo.Notifications", "ProductOptionId");
CreateIndex("dbo.Notifications", "PaymentId");
AddForeignKey("dbo.Notifications", "PaymentId", "dbo.Payments", "Id", cascadeDelete: true);
}
编辑:我将更改恢复到错误发生之前。我将 ProductOptionId 属性添加到模型中,添加迁移并成功更新数据库。
当我将 PaymentId 属性更改为可为 null 时,似乎会出现问题。
这有意义吗?
最佳答案
1- 确保迁移后将表创建到数据库中
2- 确保上下文引用您已迁移到的同一数据库
关于c# - 为 MySql 数据库运行 update-database 时出现错误 : Table 'xxx' doesn't exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43372571/