c# - 为 MySql 数据库运行 update-database 时出现错误 : Table 'xxx' doesn't exist

标签 c# mysql ef-code-first

我最近更改了类中的一些属性(通知),并且像往常一样,我为更改添加了迁移。 我运行 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/

相关文章:

c# - 使用开关的菜单不接受我的输入,在 powershell 中?

c# - 将 System.Data.Entity.DynamicProxies 转换为 C# 中的(非代理)类

c# - 正则表达式替换不会替换所有出现的地方

php - 当我单独运行 PDO 脚本时,它工作正常,但当我将它放入函数中时,它无法工作

mysql - 多表 DELETE 语句是不好的做法吗?为什么?

c# - 在c#中获取字符串的第二个单词

php - 无法添加或更新子行 : a foreign key constraint fails on real server

c# - 当一列值依赖于另一列值时 Entity Framework 代码首先约束

c# - 使用 Entity Framework 处理自定义字段

c# - Entity Framework ,鉴别器列,但没有继承