解决 Mysql下使用EF Code First 指定表Engine无效的思路
用Code First創(chuàng)建表時 用 update-database -verbose 查看腳本 可以發(fā)現(xiàn)所有的表的默認Engine都是InnoDB
因為業(yè)務(wù)要求 有張表的Engine是MyISAM.
到Migration的up方法中修改
未修改前的代碼如下:
CreateTable("Tests",c => new{ID = c.Int(nullable: false),Name = c.String(unicode: false),}).PrimaryKey(t => t.ID);想辦法從CreateTable方法入手 查看基類CreateTable方法說明
//// 摘要:// Adds an operation to create a new table.//// 參數(shù):// name:// The name of the table. Schema name is optional, if no schema is specified// then dbo is assumed.//// columnsAction:// An action that specifies the columns to be included in the table. i.e. t// => new { Id = t.Int(identity: true), Name = t.String() }//// anonymousArguments:// Additional arguments that may be processed by providers. Use anonymous type// syntax to specify arguments e.g. 'new { SampleArgument = "MyValue" }'.//// 類型參數(shù):// TColumns:// The columns in this create table operation. You do not need to specify this// type, it will be inferred from the columnsAction parameter you supply.//// 返回結(jié)果:// An object that allows further configuration of the table creation operation.protected internal TableBuilder<TColumns> CreateTable<TColumns>(string name, Func<ColumnBuilder, TColumns> columnsAction, object anonymousArguments = null);好了 anonymousArguments參數(shù)是我可以用的 根據(jù)說明 本以為應(yīng)該是 new { Engine = "MyISAM" }
修改代碼后 如下:
CreateTable("Tests",c => new{ID = c.Int(nullable: false),Name = c.String(unicode: false),}, new { Engine = "MyISAM" }).PrimaryKey(t => t.ID);再update-database -verbose 看腳本 結(jié)果還是一樣 默認InnoDB的?
當(dāng)時估計 可能是PropertyName 區(qū)分大小寫 又試了?new { engine = "MyISAM" }?new { ENGINE = "MyISAM" }
還是不行. nnd mysql關(guān)于code first的資料太少了 官網(wǎng)上都是些簡單的demo
沒辦法 去下載Connector的源碼 自己看看 源碼下載點擊這里
打開項目MySql.Data.Entity中的MySqlMigrationSqlGenerator.cs
protected virtual MigrationStatement Generate(CreateTableOperation op){StringBuilder sb = new StringBuilder();if (generatedTables == null)generatedTables = new List<string>();if (!generatedTables.Contains(op.Name)){generatedTables.Add(op.Name);}sb.Append("create table " + "`" + op.Name + "`" + " (");//columnssb.Append(string.Join(",", op.Columns.Select(c => "`" + c.Name + "` " + Generate(c))));if (op.PrimaryKey != null && !sb.ToString().Contains("primary key")){sb.Append(",");sb.Append("primary key ( " + string.Join(",", op.PrimaryKey.Columns.Select(c => "`" + c + "`")) + ") ");}sb.Append(") engine=InnoDb auto_increment=0");return new MigrationStatement() { Sql = sb.ToString() };}搞了半天 原來根本沒有考慮到指定Engine的情況.
到這里就沒辦法繼續(xù)了 我只是提供思路 修改源碼
if (op.AnonymousArguments.ContainsKey("Engine")){sb.Append(string.Format(") engine={0} auto_increment=0", op.AnonymousArguments["Engine"]));}else{sb.Append(") engine=InnoDb auto_increment=0");}然后再用 new { Engine = "MyISAM" }就沒有問題了
稍后我會向mysql 提交bug報告 各位同學(xué)也可以等待mysql來修復(fù)
已經(jīng)向mysql提交功能缺失bug報告?Bug?#68237
?
轉(zhuǎn)載于:https://www.cnblogs.com/akini/archive/2013/01/31/2887393.html
總結(jié)
以上是生活随笔為你收集整理的解决 Mysql下使用EF Code First 指定表Engine无效的思路的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何解决linux的ssh连接自动断开的
- 下一篇: linux并发控制之原子操作