Table 'DBNAME.dbo.TableNAME' doesn't exist Entity Framework 6 with MySQL

断了今生、忘了曾经 提交于 2019-12-04 15:03:10
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());

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>();
    }
}
mlemanczyk

The solution given in How can I stop Entity Framework 5 migrations adding dbo. into key names? may work for you. It did for me in several cases.

After 10 months since you asked this question you may have your own approach. Based on my experience I don't recommend using automatic migrations, at least not with MySQL. There are several incompatibilities you need to resolve manually. When using automatic migrations, you can easily break your database and spend hours trying to fix it, if you don't want to loose the data.

The issues include:

  • incorrect support for column renames
  • no support for index renames
  • changes to code-first model are reflected incorrectly in the migrations and you need to tune them

I have solutions to the first 2, if you are interested. The last one is always manual step.

The approach that worked for me 100% so far was to follow the procedure:

  1. Make changes to code-first model
  2. Generate new migration using Add-Migration
  3. Review the generated code and make sure it's correct
  4. Substitute column/index operations with your fixed versions
  5. Apply the migration
Frederico Ribeiro

I had the same error, I am using MySQL and CodeFirst. I just deleted the database and restarted the application. This solved the problem for me.

This occurs because the table names begin with dbo. in DropIndex or DropForeignKey statements. If you do

Update-Database -Verbose

you'll see the generated SQL & the precise error message:

Can't DROP 'FK_Bunnies_dbo.Carrots_Carrot_CarrotId'; check that column/key exists

the actual FK name being

FK_Bunnies_Carrots_Carrot_CarrotId

It seems that the presence of the dbo. particle only affects DropIndex, DropForeignKey statements (I am running EF + MySQL). CreateIndex, AddForeignKey are not affected by this!

So, the solution is to replace dbo. with nothing in ALL parameters passed to the aforementioned functions.

I had this issue today with this code.

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

Removing the dbo.solved my problem. i had this . I changed it to

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

And everything work ! I got the idea from the @andypopa answer.

Is your schema actually dbo? dbo is default in EF. You can change this on the entity by using the Table attribute and providing a new Schema name.

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

Replace all "dbo." in your migrations to "".

That is, just remove ".dbo"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!