Dapper抛出“DynamicMethod的无效类型所有者”。

时间:2015-05-25 09:32:23

标签: c# dapper

所以我正在尝试使用Dapper.net,我很喜欢它。我不喜欢的是当我尝试批量插入实体时,我得到以下错误抛出:

DynamicMethod的无效类型所有者。

  

at System.Reflection.Emit.DynamicMethod.Init(String name,   MethodAttributes属性,CallingConventions callingConvention,   键入returnType,类型[]签名,类型所有者,模块m,布尔值   skipVisibility,Boolean transparentMethod,StackCrawlMark&amp; stackMark)   在System.Reflection.Emit.DynamicMethod..ctor(String name,Type   returnType,Type [] parameterTypes,Type owner,Boolean skipVisibility)   在Dapper.SqlMapper.CreateParamInfoGenerator(身份标识,   Boolean checkForDuplicates,Boolean removeUnused,IList 1 literals) in D:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 3033 at Dapper.SqlMapper.GetCacheInfo(Identity identity, Object exampleParameters, Boolean addToCache) in D:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 2138 at Dapper.SqlMapper.<QueryImpl>d__61 1.MoveNext()in   D:\ Dev \ dapper-dot-net \ Dapper NET40 \ SqlMapper.cs:1578行at   System.Collections.Generic.List 1..ctor(IEnumerable 1集合)
  在System.Linq.Enumerable.ToList [TSource](IEnumerable 1 source) at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable 1   commandTimeout,Nullable 1 commandType) in D:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 1479 at Dapper.SqlMapper.Query(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable 1   commandTimeout,Nullable 1 commandType) in D:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 1418 at NinjaEvaluation.Data.Database.DapperWrapper.<>c__DisplayClass4 1.b__3(SqlConnection   sqlConnection,SqlTransaction事务)中   C:\项目\点播服务\ ninjaevaluation \ NinjaEvaluation \ NinjaEvaluation.Data \数据库\ DapperWrapper.cs:线   52在NinjaEvaluation.Data.Database.DapperWrapper.Invoke(Action`2   行动)   C:\项目\点播服务\ ninjaevaluation \ NinjaEvaluation \ NinjaEvaluation.Data \数据库\ DapperWrapper.cs:线   68

当我像这样运行查询时,会发生完全正常的情况:

        string sql = @" INSERT INTO XXX
                        (XXXId, AnotherId, ThirdId, Value, Comment)
                        VALUES
                        (@XXXId, @AnotherId, @ThirdId, @Value, @Comment)";

        var parameters = command
            .MyModels
            .Select(model => new
            {
                XXXId= model.XXXId,
                AnotherId= model.AnotherId,
                ThirdId= model.ThirdId,
                Value = model.Value,
                Comment = model.Comment
            })
            .ToArray();

...

sqlConnection.Query(sql, parameters, commandType: commandType, transaction: transaction)

我发现the following SO-thread由有同样问题的人启动但是问题似乎是.NET版本(3.5)但我运行.NET 4.5而我无法弄清楚问题是什么是

有什么建议吗?

3 个答案:

答案 0 :(得分:9)

它失败,因为使用Query[<T>]的这种情况不期望参数的数组/序列。 Execute调用路径确实期望这一点,并自动展开数据,每个项目执行一次SQL - 但Query[<T>]不是这种情况,所以尝试创建绑定到数组的动态方法(在您的情况下),这是不允许的。代码可能应该更早地检测到这一点,并且只是说“不,这是不允许的”。

可能想要将.ToArray()更改为.Single()

下一次构建后会更清楚;以下过程:

    public void SO30435185_InvalidTypeOwner()
    {
        try {
            // not shown for brevity: something very similar to your code
            Assert.Fail();
        } catch(InvalidOperationException ex)
        {
            ex.Message.IsEqualTo("An enumerable sequence of parameters (arrays, lists, etc) is not allowed in this context");
        }
    }

答案 1 :(得分:7)

Query强烈输入集合时遇到此错误,如下所示:Query<IEnumerable<string>>

何时应该只有Query<string>

请注意,上述说明可能不正确,请参阅@ Ryan的回答

答案 2 :(得分:5)

使用界面而不是类时遇到此错误:

Query<MyObject>有效,而Query<IMyObject>没有