数据库中没有表 'DBNAME.dbo.TableNAME'。使用MySQL的Entity Framework 6。

7

我正在使用Entity Framework 6.0.0和MySql Server 5.6.17。

我通过nuget添加了MySql.Data.Entities,并安装了Entity Framework 6.0.0和MySql.Data 6.8.4。

一切都设置得很好,我的一些业务实体也正常工作,它启用了自动迁移(true)。

后来我添加了一些实体,然后它开始出现错误,如下:

Table 'DBNAME.dbo.TABLENAME' doesn't exist Entity Framework 6
我尝试删除整个数据库并重新创建,但没有起作用。 我尝试将Entity Framework更新到6.1.2和MySql.Data更新到6.9.5,但没有解决问题,反而出现了一些其他错误,例如:
Method not found: 'System.Data.Entity.Migrations.Builders.TableBuilder`1<!0> System.Data.Entity.Migrations.Builders.TableBuilder`1.Index(System.Linq.Expressions.Expression`1<System.Func`2<!0,System.Object>>, System.String, Boolean, Boolean, System.Object)'.

我已经将我的EF和MySql.Data更改为之前的版本(EF 6.0.0和MySql.Data 6.8.4)

我发现了另一篇文章http://bugs.mysql.com/bug.php?id=69649与我遇到了相同的错误,因此我按照以下方式修改了我的配置方法

 public Configuration()
 {
            this.AutomaticMigrationsEnabled = true;
            SetSqlGenerator("MySql.Data.MySqlClient", new MySql.Data.Entity.MySqlMigrationSqlGenerator());
            CodeGenerator = new MySql.Data.Entity.MySqlMigrationCodeGenerator();
            AutomaticMigrationDataLossAllowed = true;  // or false in case data loss is not allowed.
 }

但是它没有解决问题。我再次遇到了相同的错误。

我的示例业务实体如下。

public class User
{
    [Key]
    public int UserID { get; set; }
    [Display(Name = "User Email")]
    [AllowHtml]
    public string UserEmail { get; set; }
    [MaxLength(100)]
    [Required(ErrorMessage = "Enter user password")]
    [Display(Name = "User Password")]
    [AllowHtml]
    public string UserPassword { get; set; }
    [MaxLength(50)]
    [Display(Name = "First Name")]
    [AllowHtml]
    public string FirstName { get; set; }
    [MaxLength(50)]
    [Display(Name = "Last Name")]
    [AllowHtml]
    public string LastName { get; set; }
    [Display(Name = "Profile Picture")]
    public string ProfilePicURL { get; set; }
    public string SaltKey { get; set; }
}

提前感谢您的任何帮助


你的问题被标记为“mysql”。然而,你正在使用与SQL Server相关联的三部分命名约定。请使用正确的MySQL数据库引用或连接到SQL Server数据库。 - Gordon Linoff
@GordonLinoff 这就是我在错误信息中看到的。据我所知,我没有指定任何类似的东西。 - Arjun Vachhani
我也看到了这个错误。你最后解决了吗? - OneHoopyFrood
7个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
12
class SqlGenerator : MySql.Data.Entity.MySqlMigrationSqlGenerator
{
    public override IEnumerable<MigrationStatement> Generate(IEnumerable<MigrationOperation> migrationOperations, string providerManifestToken)
    {
        IEnumerable < MigrationStatement > res =  base.Generate(migrationOperations, providerManifestToken);
        foreach(MigrationStatement ms in res)
        {
            ms.Sql = ms.Sql.Replace("dbo.","");
        }
        return res;
    }
}
(...)
SetSqlGenerator("MySql.Data.MySqlClient", new SqlGenerator());
更好的解决方案: 当我重新启动应用程序时,EF总是想再次删除和创建外键约束,所有这些奇怪的SQL操作都包含“dbo.”模式。 我只是忽略它们。
foreach(MigrationStatement ms in res)
{
    //ms.Sql = ms.Sql.Replace("dbo.","");
    if(ms.Sql.Contains("dbo."))
    {
        return new List<MigrationStatement>();
    }
}

1
你能否在回答中再加入一些细节呢?谢谢。 - maxshuty
1
@MichalB 你所说的是正确的。可以在 https://bugs.mysql.com/bug.php?id=69649 找到更多信息。 - Arjun Vachhani

3
如何停止Entity Framework 5迁移将dbo添加到关键字中?提供的解决方案可能适用于您。在我的几个案例中,它确实有效。 您提出这个问题已经10个月了,您可能有自己的方法。基于我的经验,我不建议使用自动迁移,至少不适用于MySQL。存在几个需要手动解决的不兼容性。当使用自动迁移时,如果您不想损失数据,很容易破坏数据库并花费数小时尝试修复它。 这些问题包括:
  • 错误的列重命名支持
  • 不支持索引重命名
  • 对代码优先模型的更改在迁移中反映不正确,您需要调整它们
如果您有兴趣,我可以为前两个问题提供解决方案。最后一个问题始终是手动步骤。 到目前为止,对我有效的方法是遵循以下步骤:
  1. 对代码优先模型进行更改
  2. 使用Add-Migration生成新的迁移
  3. 检查生成的代码,确保它是正确的
  4. 使用您的修复版本替换列/索引操作
  5. 应用迁移

在某些情况下,情况可能会更糟,EF 生成的迁移首先重命名表,然后尝试在旧命名的表中重命名列。到目前为止,我最大的运气是使用“update-database -script”,然后逐个执行它们时微调 SQL 查询。 - aeroson

2
这是因为在DropIndex或DropForeignKey语句中,表名以dbo.开头。如果您执行这些语句,则会出现此问题。请检查并更改表名,或使用带有dbo.前缀的正确表名来解决此问题。请注意保留HTML标签。
Update-Database -Verbose

您将看到生成的SQL和详细的错误信息:

Can't DROP 'FK_Bunnies_dbo.Carrots_Carrot_CarrotId'; check that column/key exists
实际的FK名称为:
FK_Bunnies_Carrots_Carrot_CarrotId

看起来,dbo.粒子的存在只影响DropIndex、DropForeignKey语句(我正在运行EF + MySQL)。CreateIndex、AddForeignKey不受此影响!

因此,解决方案是在传递给上述函数的所有参数中将dbo.替换为空。


2
今天我遇到了这段代码的问题。
DropForeignKey("dbo.BlogPostViews", "BlogPostId", "dbo.BlogPosts");

去掉dbo后,问题得到解决。我遇到过这个问题。我将它改为

DropForeignKey("BlogPostViews", "BlogPostId", "BlogPosts");

一切都正常!我从@andypopa的答案中得到了灵感。


2
我也遇到过这个错误,我使用的是MySQL和CodeFirst。我只是删除了数据库并重新启动了应用程序。这样解决了我的问题。

问题与外键有关。这篇文章解决了这个问题:https://dev59.com/VGct5IYBdhLWcg3wgNcy - Arjun Vachhani

0

你的模式实际上是dbo吗?在EF中,dbo是默认值。您可以通过使用Table属性并提供新的Schema名称来更改实体上的模式。

[Table("TableName", Schema = "schemaName")]

0

在您的迁移中将所有 "dbo." 替换为 ""。

也就是说,只需要删除 ".dbo" 即可。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,