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

Arj*_*ani 3 c# mysql entity-framework entity-framework-6

我正在使用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
Run Code Online (Sandbox Code Playgroud)

我已经尝试删除整个数据库并重新创建它,但它没有奏效.

我已经尝试将实体框架更新到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)'.
Run Code Online (Sandbox Code Playgroud)

所以我将我的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.
 }
Run Code Online (Sandbox Code Playgroud)

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

我的样本业务实体如下.

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; }
}
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的帮助

Mic*_*ł B 9

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());
Run Code Online (Sandbox Code Playgroud)

更好的解决方案:当我重新启动应用程序时,EF总是希望再次删除并创建外键约束,所有这些奇怪的SQL操作都包含"dbo".架构.我只是忽略它们.

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