zoukankan      html  css  js  c++  java
  • 解决 Mysql下使用EF Code First 指定表Engine无效的思路

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

    因为业务要求 有张表的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.
            //
            // 参数:
            //   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" }'.
            //
            // 类型参数:
            //   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.
            //
            // 返回结果:
            //     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参数是我可以用的 根据说明 本以为应该是 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 看脚本 结果还是一样 默认InnoDB的 

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

    还是不行. nnd mysql关于code first的资料太少了 官网上都是些简单的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 + "`" + " (");
    
          //columns
          sb.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的情况.

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

          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报告 各位同学也可以等待mysql来修复  

    已经向mysql提交功能缺失bug报告 Bug #68237

  • 相关阅读:
    先不说 console,其实你连 console.log 都不会
    2019 年终总结 & 2020 年度计划
    将毫秒格式化为天、小时、分钟、秒
    山村老事
    快速更改对象中的字段名
    基于 ECharts 封装甘特图并实现自动滚屏
    JS 将数值取整为10的倍数
    Flutter 徐徐图之(一)—— 从搭建开发环境到 Hello World
    Vue-Cli 3.x 创建的项目中对 import 引入的 CSS 样式启用 autoprefixer
    word——插入目录
  • 原文地址:https://www.cnblogs.com/akini/p/2887393.html
Copyright © 2011-2022 走看看