Bug #67773 Migration on entity framework 5.0
Submitted: 30 Nov 2012 19:34 Modified: 22 Feb 2013 3:29
Reporter: Re Porcello Email Updates:
Status: No Feedback Impact on me:
None 
Category:Connector / NET Severity:S1 (Critical)
Version:6.6.4.0 OS:Windows
Assigned to: Assigned Account CPU Architecture:Any
Tags: entity framework, migration

[30 Nov 2012 19:34] Re Porcello
Description:
i'm trying to use migration on entity framework 5.0..
i got a TargetInvocationException
Could not load file or assembly 'EntityFramework, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

i've just found this page on the web about this issue:
http://stackoverflow.com/questions/12380147/mysql-5-5-net-connector-entity-framework-migra...

How to repeat:
Using visual studio 2012 web express,
i've a code first model and a mysql database correcly configurated.
i've activated migration (in the package manager console Enable-Migrations)
then add-migration migrationName to add a migration.
i've edited the configuration to user the MySqlMigrationSqlGenerator as follow.

internal sealed class Configuration : DbMigrationsConfiguration<AnnounceRegisterUltra.Lib.Model.Entities>
{
     public Configuration()
     {
         AutomaticMigrationsEnabled = false;
         SetSqlGenerator("Migrator", new MySql.Data.Entity.MySqlMigrationSqlGenerator());
     }
...
}

and finally i ran the command update-database
[9 Dec 2012 9:05] Patrick Schimmel
I've got a similar problem.

I'm using EF Code First Migration on EF 5.0 (4.4) and the .NET Framework 4.0 with VS 2010.

Here's the error message:

No MigrationSqlGenerator found for provider 'MySql.Data.MySqlClient'. Use the SetSqlGenerator method in the target migrations configuration class to register additional SQL generators.

Is there a way to register the requested generator?
Without migrations enabled the code first approach is rather useless to me.
[9 Dec 2012 9:14] Patrick Schimmel
By adding the following line into the constructor of the Configuration class I've come one step forward:

SetSqlGenerator("MySql.Data.MySqlClient", new MySqlMigrationSqlGenerator());

But now I receive the following error message when trying to do the migration:
Method not found: 'System.DateTime System.Data.Entity.Migrations.Model.InsertHistoryOperation.get_CreatedOn()'.
[20 Dec 2012 8:29] json j
This bug caused by the '__migrationhistory' table has no column named 'createon'.

Override the method MigrationStatement Generate(InsertHistoryOperation op) can fix it.

public class CustomMySqlMigrationSqlGenerator : MySqlMigrationSqlGenerator
    {
        protected override MigrationStatement Generate(InsertHistoryOperation op)
        {
            if (op == null) return null;

            StringBuilder sb = new StringBuilder();
            StringBuilder model = new StringBuilder();

            foreach (byte item in op.Model)
                model.Append(item.ToString("X2"));
            
            //mysql can't find table which name startwith '__',
            //so we have to replace it like 'dbo.__'
            string tableName = op.Table;
            if (!tableName.StartsWith("dbo."))
            {
                tableName = "dbo." + tableName;
                Console.WriteLine("Auto fix table name");
            }

            sb.Append("insert into `" + tableName + "` (`migrationId`, `model`, `productVersion`) ");
            sb.AppendFormat(" values ( '{0}', {1}, '{2}' ) ",
                            op.MigrationId,
                            "0x" + model,
                            op.ProductVersion);

            return new MigrationStatement { Sql = sb.ToString() };
        }

    }
[21 Dec 2012 18:35] Patrick Schimmel
Thanks for the help. Now the database is created.
However every table name receives the schema name or "dbo" as prefix if the schema name is not defined as mapping.
The class Activity for instance is mapped as dbo.activities.
After that the Entity Framework will not find the table anymore.
[22 Jan 2013 3:29] Gabriela Martinez Sanchez
Hi P. Schimmel,
Thanks for your interest and feedback on Connector/Net. This issue about the "dbo." table prefix added on every table name is due to some changes done on EF 5. Which now is adding by default this prefix no matter of the provider. We're close to get a new version which is supporting EF 5 and that doesn't have this problem.
Please we encourage you to check our forums(http://forums.mysql.com/list.php?38) and our blog (https://blogs.oracle.com/MySqlOnWindows/) where we'll  be doing the announcements of the release of this new version.

Thanks
[23 Feb 2013 1:00] Bugs System
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".