使用 MySQL .Net Core ADO 执行多查询

Executing multi query using MySQL .Net Core ADO

我正在尝试为新的 asp.net 核心站点创建 MySQL 数据库。我在 MVC5 时代使用了类似的模式,使用 Datatable 和 DataSet。那些可怕的数据结构已从 Core 中删除,我想知道执行具有多个结果集的查询的最佳方法是什么。

例如,

  public static IEnumerable<IEnumerable<IDataRecord>> ExecuteMultiQuery(MySqlConnection connection, CommandType commandType, string commandText, params MySqlParameter[] commandParameters) {

        var returnObject = new List<List<IDataRecord>>();

        using (MySqlCommand command = PrepareCommand(connection, commandType, commandText, commandParameters)) {
            try {
                connection.Open();

                command.Prepare();

                MySqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);

                do {

                    List<IDataRecord> resultSet = new List<IDataRecord>();

                    while (reader.Read()) {
                        resultSet.Add(reader);
                    }

                    returnObject.Add(resultSet);

                } while (reader.NextResult());

            }
            finally {
                connection.Close();
            }

            command.Parameters.Clear();

        }

        return returnObject;
    }

但是,在逐步执行代码时,我注意到这些集合实际上从未保存任何数据。我已经测试了我的存储过程,这正确 returns 需要的行。此外,我还有一个类似于 Dapper 的定制微型 ORM,它负责 POCO 绑定。我对此很满意,不想依赖外部 nuget 包。

我只是不明白为什么该方法没有填充集合...

任何指导将不胜感激。

P.S PrepareCommand 方法调用是私有的,只是 returns 一个正确填充了参数的 MySqlCommand。

P.P.S 不要向我提及 EF,因为这是一个可怕的框架,绑定时间很长。我可以在大约 75-90 毫秒内以类似的 ExecuteReader 方法绑定模型(不是 Dapper 快但足够快)所以我知道我创建的 DBUtiltiy 在其他场景中有效 - 只是不适用于多查询。

谢谢大家

嗯,已经有一段时间了,这里只翻滚杂草。但我只能建议等待新版本的 Core,它将包括旧的 DataSet、DataTable 库以及 Transactions!

作为解决方法,我 return 一个 IEnumerable 并使用反射来填充 POCO。脏,但在 Core 成为一个完全形成的框架之前,它会作为一种妥协。