Paceman Paceman - 2 days ago 5
MySQL Question

EF and Code First Migrations with MySQL - dbo.tablename does not exist

I have set up entity framework to use MySQL and created a migration.

When I run update-database I get error "table dbname.dbo.tablename' does not exist. Running in -Verbose mode I see the statement that causes the error :

alter table `dbo.Comments` drop foreign key `FK_dbo.Comments_dbo.Comments_Comment_ID`


When I run the query direct in MySQL workbench is throws the same error.

The problem seems to be the dbo. prefix in the migration set. Anything in the form dbo.tablename won't run saying that table does not exist. E.g. select * from dbo.tablename fails but select * from tablename works. The database was generated by Entity Framework and the code first migrations were generated by EF too.

However the migrations generate everything with the dbo. prefix, which does not work.

Does anyone have a solution to this?

Answer

I was having this problem just today as well; found my answer here: MySqlMigrationCodeGenerator

You have to set:

CodeGenerator = new MySqlMigrationCodeGenerator();

In your context's configuration class. This will get rid of the schema gibberish for MySQL. My Configuration class looks like this:

internal sealed class Configuration : DbMigrationsConfiguration<YourContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
        SetSqlGenerator("MySql.Data.MySqlClient", new MySqlMigrationSqlGenerator());
        SetHistoryContextFactory("MySql.Data.MySqlClient", (conn, schema) => new MySqlHistoryContext(conn, schema));
        CodeGenerator = new MySqlMigrationCodeGenerator();
    }
}
Comments