activerecord - 无法在 Subsonic3 中批量插入,错误为 "Must declare the scalar variable..."

标签 activerecord subsonic3

我遇到了关于使用 Subsonic3 批量插入多行的问题。我的开发环境包括:

1. Visual Studio 2010, but use .NET 3.5
2. Active Record Mode in SubSonic 3.0.0.4
3. SQL Server 2005 express
4. Northwind sample database

我正在使用 Active Reecord 模式将多个“产品”插入到“产品”表中。如果我一一插入行,调用“aProduct.Add()”或多次调用“Insert.Execute()”(就像下面的代码一样),它工作正常。
        private static Product[] CreateProducts(int count)
        {
            Product[] products = new Product[count];
            for (int index = 0; index < products.Length; ++index)
            {
                products[index] = new Product
                {
                    ProductName = string.Format("cheka-test-{0}", index.ToString()),
                    Discontinued = (index % 2 == 0),                        
                };
            }
            return products;
        }
        private static void SucceedByMultiExecuteInsert()
        {
            Product[] products = CreateProducts(2);

            // -------------------------------- prepare batch
            NorthwindDB db = new NorthwindDB();

            var inserts = from prod in products
                          select db.Insert.Into<Product>(x => x.ProductName, x => x.Discontinued).Values(prod.ProductName, prod.Discontinued);

            // -------------------------------- batch insert
            var selectAll = Product.All();
            Console.WriteLine("--- before total rows = {0}", selectAll.Count().ToString());

            foreach (Insert insert in inserts)
                insert.Execute();

            Console.WriteLine("+++ after inserting {0} rows, now total rows = {1}",
                products.Length.ToString(), selectAll.Count().ToString());
        }

但是如果我像下面的代码一样使用“BatchQuery”,
    private static void FailByBatchInsert()
    {
        Product[] products = CreateProducts(2);

        // -------------------------------- prepare batch
        NorthwindDB db = new NorthwindDB();
        BatchQuery batchquery = new BatchQuery(db.Provider, db.QueryProvider);

        var inserts = from prod in products
                      select db.Insert.Into<Product>(x => x.ProductName, x => x.Discontinued).Values(prod.ProductName, prod.Discontinued);

        foreach (Insert insert in inserts)
            batchquery.Queue(insert);

        // -------------------------------- batch insert
        var selectAll = Product.All();
        Console.WriteLine("--- before total rows = {0}", selectAll.Count().ToString());

        batchquery.Execute();

        Console.WriteLine("+++ after inserting {0} rows, now total rows = {1}",
            products.Length.ToString(), selectAll.Count().ToString());
    }

然后它失败了,异常:

未处理的异常:System.Data.SqlClient.SqlException:必须声明标量变量“@ins_ProductName”。
必须声明标量变量“@ins_ProductName”。



请给我一些帮助来解决这个问题。非常感谢。

最佳答案

我也遇到了这个问题。如果您查看它尝试运行的查询,您会看到它执行以下操作(这不是实际代码,但您会明白这一点):

exec_sql N'insert into MyTable (SomeField) Values (@ins_SomeField)',N'@0 varchar(32)','@0=SomeValue'

出于某种原因,它使用 "@ins_"+FieldName 定义了查询中的参数。但随后将参数作为序数传递。我还没有确定为什么/何时这样做的模式,但在这个开发周期中我已经失去了足够的时间来尝试正确诊断问题。

我实现的解决方法将涉及您从 github 下载 3.0.0.4 源代码并在 Insert.cs 的第 179 行进行更改。

它读到的地方
ParameterName = _provider.ParameterPrefix + "ins_" + columnName.ToAlphaNumericOnly(),

将其更改为
ParameterName = _provider.ParameterPrefix + Inserts.Count.ToString(),

似乎对我有用。我对您的此解决方案不作任何明示或暗示的保证。它确实对我有用,但您的里程可能会有所不同。

我还应该注意到,在 Update.cs 的第 181 和 194 行的“更新”语句中也有类似的逻辑,但我还没有遇到这些给我带来的问题......

老实说,我认为 SubSonic 还没有准备好迎接黄金时段,这很遗憾,因为我真的很喜欢 Rob 的设置方式。也就是说,它现在在我的产品中是好是坏,所以你可以充分利用你所拥有的。

关于activerecord - 无法在 Subsonic3 中批量插入,错误为 "Must declare the scalar variable...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3120501/

相关文章:

mysql - 亚音速 3,MySQL T4 模板 : Structs. tt 失败

subsonic3 - Subsonic 3 简单存储库和事务

简单亚音速示例的 SQL 错误

SubSonic SimpleRepository - 默认值

ruby-on-rails - 使用named_scope的加性链接

ruby-on-rails - 在链接表上 CRUD 额外属性的正确 Rails 方法

ruby-on-rails - 在ActiveRecord子类中覆盖 '=='方法有意义吗?

ruby-on-rails - 在rails中创建一个表并添加外键约束

ruby-on-rails - Mongoid 相当于 ActiveRecord 的 `first_or_initialize`

mysql - 使用 SubSonic 检索一列值的最简单方法是什么(列不是主要的)