表 'DBNAME.dbo.TableNAME' 不存在 MySQL 的实体框架 6

Table #39;DBNAME.dbo.TableNAME#39; doesn#39;t exist Entity Framework 6 with MySQL(表 DBNAME.dbo.TableNAME 不存在 MySQL 的实体框架 6)
本文介绍了表 'DBNAME.dbo.TableNAME' 不存在 MySQL 的实体框架 6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Entity Framework 6.0.0 with MySql Server 5.6.17

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

I have added MySql.Data.Entities through nuget and it installed Entity Framework 6.0.0 and MySql.Data 6.8.4

一切都设置得很完美,并且可以与我的一些业务实体正常工作.它启用了自动迁移(true).

Everything was setup perfectly and working fine with some of my Business Entities. it have automigration enabled(true).

后来我添加了更多实体,然后它开始给出错误,例如

Later i have added some more entities and then it started giving error like

Table 'DBNAME.dbo.TABLENAME' doesn't exist Entity Framework 6

我尝试删除整个数据库并重新创建它,但没有成功.

I have tried deleting whole database and recreate it again, but it didn't worked.

我尝试将 Entity Framework 更新到 6.1.2 并将 MySql.Data 更新到 6.9.5,但它没有解决问题,但出现了一些其他错误,例如

I have tried updating Entity Framework to 6.1.2 and MySql.Data to 6.9.5 but it did not solved problem but gives some other error like

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)

So i changed my EF and MySql.Data to previous version(EF 6.0.0 And MySql.Data 6.8.4)

我又找到一篇文章 http://bugs.mysql.com/bug.php?id=69649 谁和我有同样的错误所以我修改了我的配置方法如下

I found one more article http://bugs.mysql.com/bug.php?id=69649 who has same error like me so i modified my configuration method as below

 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.
 }

但它没有解决问题.我又遇到了同样的错误.

but it did not solved issue. i got same error again.

我的示例业务实体如下.

My Sample Business Entity is as below.

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; }
}

提前感谢您的帮助

推荐答案

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".架构.我只是忽略它们.

Better solution: When i restart application, EF always want to drop and create foreign key constraint one more time, all this strange sql operations contains "dbo." schema. I just ignore them.

foreach(MigrationStatement ms in res)
{
    //ms.Sql = ms.Sql.Replace("dbo.","");
    if(ms.Sql.Contains("dbo."))
    {
        return new List<MigrationStatement>();
    }
}

这篇关于表 'DBNAME.dbo.TableNAME' 不存在 MySQL 的实体框架 6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)