【问题标题】:Getting error when running update-database for MySql database: Table 'xxx' doesn't exist为 MySql 数据库运行更新数据库时出现错误:表 'xxx' 不存在
【发布时间】:2017-09-08 09:55:20
【问题描述】:

我最近更改了班级(通知)中的一些属性,并且像往常一样添加了更改的迁移。 我运行了 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 属性更改为可空时,问题似乎正在发生。

这有意义吗?

【问题讨论】:

  • 这看起来不像 MySQL,这似乎是 ms sql server。
  • @Shadow 是什么让你这么认为?
  • xxx.dbo.notifications - 在 MySQL 中,您可以有 xxx.notifications 表或 xxx.dbo 表,但路径中不能有 3 个元素。表只与数据库关联(模式)。
  • 我可以向你保证,它是 MySQL。我在它的迁移配置 cos 中有以下行: SetSqlGenerator("MySql.Data.MySqlClient", new MySql.Data.Entity.MySqlMigrationSqlGenerator());这是我第一次遇到这个问题
  • 你的数据库可能是MySQL,但是报错信息中的表名不能存在于MySQL中。期间。

标签: c# mysql ef-code-first


【解决方案1】:

1- 确保在迁移后将表创建到数据库中
2- 确保上下文引用的是您已迁移到的同一数据库

【讨论】:

  • 该表之前已经存在。我只是更改了一些属性并添加了新属性。我也只使用一种上下文。
  • 您是如何添加属性的?从您的模型类,然后您进行迁移或直接从您的数据库管理,我的意思是确保在运行应用程序之前上下文连接字符串不会动态更改
  • 我从我的模型类中添加了属性,并且在我进行了迁移之后。我从来没有从我的数据库管理中接触过它,所以我不会造成任何问题。
  • 你能检查一下你的迁移脚本文件让我看看,它可能会误删除表
猜你喜欢
  • 2014-02-27
  • 2021-10-03
  • 2020-09-22
  • 1970-01-01
  • 2012-10-04
  • 2020-04-02
  • 1970-01-01
  • 2018-10-10
  • 2012-08-16
相关资源
最近更新 更多