Description:
Concenated Create Table Statements are corrupted by missing semicolon and causes an interpretation error on MySQL Server side.
How to repeat:
1. Create Context:
public class Context: CoreContext.CoreContextBase
{
public Context():base("DefaultConnection"){}
public DbSet<Property1> Propertyx { get; set; }
public override string DefaultSchema { get; set;} = "Context1";
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
var mb=modelBuilder.HasDefaultSchema(DefaultSchema);
}
}
2. Create Configuration (through enable-migration, with added MySqlMigrationSqlGenerator):
public class Context1DbConfig : DbMigrationsConfiguration<Context>
{
public Context1DbConfig()
{
SetSqlGenerator("MySql.Data.MySqlClient", new MySql.Data.Entity.MySqlMigrationSqlGenerator());
AutomaticMigrationsEnabled = true;
MigrationsDirectory = @"Context1Migrations";
}
protected override void Seed(DiffDbContextTest.Model1.Context context)
{
}
}
3. Add-migration initial
4. Run following code:
Context1DbConfig configuration = new Context1DbConfig();
var migrator = new System.Data.Entity.Migrations.DbMigrator(configuration);
var scriptor = new System.Data.Entity.Migrations.Infrastructure.MigratorScriptingDecorator(migrator);
string sql = scriptor.ScriptUpdate(null, null);
Finaly:
The sql string gives:
create table `Context1.Property1` (`Id` int not null auto_increment ,`Text` longtext,`test` int not null ,primary key ( `Id`) ) engine=InnoDb auto_increment=0
create table `Context1.__MigrationHistory` (`MigrationId` nvarchar(100) not null ,`ContextKey` nvarchar(200) not null ,`Model` longblob not null ,`ProductVersion` nvarchar(32) not null ,primary key ( `MigrationId`,`ContextKey`) ) engine=InnoDb auto_increment=0
INSERT INTO .....
Should be:
create table `Context1.Property1` (`Id` int not null auto_increment ,`Text` longtext,`test` int not null ,primary key ( `Id`) ) engine=InnoDb auto_increment=0;
create table `Context1.__MigrationHistory` (`MigrationId` nvarchar(100) not null ,`ContextKey` nvarchar(200) not null ,`Model` longblob not null ,`ProductVersion` nvarchar(32) not null ,primary key ( `MigrationId`,`ContextKey`) ) engine=InnoDb auto_increment=0;
INSERT INTO .....
Suggested fix:
MySqlMigrationSqlGenerator.cs:
Add following Method to class MySqlMigrationSqlGenerator:
protected string EndsWithSemicolon(string str)
{
//remove last linefeed or whitespace end of string
string Ret = str.TrimEnd(new char[] { ' ', '\r', '\n', ';' });
Ret += ";";
return Ret;
}
Modify last line of each Generate Method to:
string sql= sb.ToString() +..... ; //dependend on Method
return new MigrationStatement { Sql=EndsWithSemicolon(sql)};