Dapper 投掷 "Invalid type owner for DynamicMethod."

Dapper throws "Invalid type owner for DynamicMethod."

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

DynamicMethod 的类型所有者无效。

at System.Reflection.Emit.DynamicMethod.Init(String name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] signature, Type owner, Module m, Boolean skipVisibility, Boolean transparentMethod, StackCrawlMark& stackMark) at System.Reflection.Emit.DynamicMethod..ctor(String name, Type returnType, Type[] parameterTypes, Type owner, Boolean skipVisibility) at Dapper.SqlMapper.CreateParamInfoGenerator(Identity identity, Boolean checkForDuplicates, Boolean removeUnused, IList1 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__611.MoveNext() in D:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 1578 at System.Collections.Generic.List1..ctor(IEnumerable1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable1 commandTimeout, Nullable1 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, Nullable1 commandTimeout, Nullable1 commandType) in D:\Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:line 1418 at NinjaEvaluation.Data.Database.DapperWrapper.<>c__DisplayClass41.b__3(SqlConnection sqlConnection, SqlTransaction transaction) in c:\Projects\InHouse\ninjaevaluation\NinjaEvaluation\NinjaEvaluation.Data\Database\DapperWrapper.cs:line 52 at NinjaEvaluation.Data.Database.DapperWrapper.Invoke(Action`2 action) in c:\Projects\InHouse\ninjaevaluation\NinjaEvaluation\NinjaEvaluation.Data\Database\DapperWrapper.cs:line 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) 但我 运行ning .NET 4.5 我可以弄清楚问题是什么。

有什么建议吗?

它失败了,因为这个使用 Query[<T>] 的场景不需要参数数组/序列。 Execute 调用路径 确实 期望这一点,并自动展开数据,每个项目执行一次 SQL - 但 [= 并非如此11=],因此它会尝试创建绑定到 array(在您的情况下)的动态方法,这是不允许的。该代码可能应该更早地检测到这一点,并且只说 "nope, that isn't allowed".

可能想要将您的.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");
        }
    }

我 运行 在使用接口而不是 class:

时出现此错误

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