MySQL下使用EF Code First指定表Engine无效时的解决办法

 用Code First创建表时 用 update-database -verbose 查看脚本 可以发现所有的表的默认Engine都是InnoDB

因为业务要求 有张表的Engine是MyISAM.

到Migration的up方法中修改

未修改前的代码如下:

 
 
  1. CreateTable( 
  2.  
  3.     "Tests"
  4.  
  5.     c => new 
  6.  
  7.         { 
  8.  
  9.             ID = c.Int(nullable: false), 
  10.  
  11.             Name = c.String(unicode: false), 
  12.  
  13.     }) 
  14.  
  15.     .PrimaryKey(t => t.ID); 

想办法从CreateTable方法入手 查看基类CreateTable方法说明

 
 
  1. // 
  2.  
  3.  // 摘要: 
  4.  
  5.  //     Adds an operation to create a new table. 
  6.  
  7.  // 
  8.  
  9.  // 参数: 
  10.  
  11.  //   name: 
  12.  
  13.  //     The name of the table. Schema name is optional, if no schema is specified 
  14.  
  15.  //     then dbo is assumed. 
  16.  
  17.  // 
  18.  
  19.  //   columnsAction: 
  20.  
  21.  //     An action that specifies the columns to be included in the table.  i.e. t 
  22.  
  23.  //     => new { Id = t.Int(identity: true), Name = t.String() } 
  24.  
  25.  // 
  26.  
  27.  //   anonymousArguments: 
  28.  
  29.  //     Additional arguments that may be processed by providers. Use anonymous type 
  30.  
  31.  //     syntax to specify arguments e.g. 'new { SampleArgument = "MyValue" }'. 
  32.  
  33.  // 
  34.  
  35.  // 类型参数: 
  36.  
  37.  //   TColumns: 
  38.  
  39.  //     The columns in this create table operation. You do not need to specify this 
  40.  
  41.  //     type, it will be inferred from the columnsAction parameter you supply. 
  42.  
  43.  // 
  44.  
  45.  // 返回结果: 
  46.  
  47.  //     An object that allows further configuration of the table creation operation. 
  48.  
  49.  protected internal TableBuilder<TColumns> CreateTable<TColumns>(string name, Func<ColumnBuilder, TColumns> columnsAction, object anonymousArguments = null); 

好了 anonymousArguments参数是我可以用的 根据说明 本以为应该是 new { Engine = "MyISAM" }

修改代码后 如下:

 
 
  1. CreateTable( 
  2.  
  3.     "Tests"
  4.  
  5.     c => new 
  6.  
  7.         { 
  8.  
  9.             ID = c.Int(nullable: false), 
  10.  
  11.             Name = c.String(unicode: false), 
  12.  
  13.         }, new { Engine = "MyISAM" }) 
  14.  
  15.     .PrimaryKey(t => t.ID); 

再update-database -verbose 看脚本 结果还是一样 默认InnoDB的 

当时估计 可能是PropertyName 区分大小写 又试了 new { engine = "MyISAM" } new { ENGINE = "MyISAM" }

还是不行. nnd mysql关于code first的资料太少了 官网上都是些简单的demo

没办法 去下载Connector的源码 自己看看 源码下载点击这里

打开项目MySql.Data.Entity中的MySqlMigrationSqlGenerator.cs

 
 
  1. protected virtual MigrationStatement Generate(CreateTableOperation op) 
  2.  
  3.  
  4.   StringBuilder sb = new StringBuilder(); 
  5.  
  6.   if (generatedTables == null
  7.  
  8.     generatedTables = new List<string>(); 
  9.  
  10.   
  11.  
  12.   if (!generatedTables.Contains(op.Name)) 
  13.  
  14.   { 
  15.  
  16.     generatedTables.Add(op.Name); 
  17.  
  18.   } 
  19.  
  20.   sb.Append("create table " + "`" + op.Name + "`" + " ("); 
  21.  
  22.   
  23.  
  24.   //columns 
  25.  
  26.   sb.Append(string.Join(",", op.Columns.Select(c => "`" + c.Name + "` " + Generate(c)))); 
  27.  
  28.   
  29.  
  30.   if (op.PrimaryKey != null && !sb.ToString().Contains("primary key")) 
  31.  
  32.   { 
  33.  
  34.     sb.Append(","); 
  35.  
  36.     sb.Append("primary key ( " + string.Join(",", op.PrimaryKey.Columns.Select(c => "`" + c + "`")) + ") "); 
  37.  
  38.   } 
  39.  
  40.   
  41.  
  42.     sb.Append(") engine=InnoDb auto_increment=0"); 
  43.  
  44.   
  45.  
  46.     return new MigrationStatement() { Sql = sb.ToString() }; 
  47.  

搞了半天 原来根本没有考虑到指定Engine的情况.

到这里就没办法继续了 我只是提供思路 修改源码

 
 
  1. if (op.AnonymousArguments.ContainsKey("Engine")) 
  2.  
  3.  
  4.     sb.Append(string.Format(") engine={0} auto_increment=0", op.AnonymousArguments["Engine"])); 
  5.  
  6.  
  7. else 
  8.  
  9.  
  10.     sb.Append(") engine=InnoDb auto_increment=0"); 
  11.  

然后再用 new { Engine = "MyISAM" }就没有问题了

稍后我会向mysql 提交bug报告 各位同学也可以等待mysql来修复  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值