Dapper throws an "Invalid Type Owner for DynamicMethod."

So, I'm trying to use Dapper.net, and I like it. I don't like it when I try to embed objects in a batch download and get the following error:

Invalid type owner for DynamicMethod.

in System.Reflection.Emit.DynamicMethod.Init (string name, MethodAttributes attributes, CallingConventions callConvention, type returnType, type [] signature, type owner, module m, logical skipVisibility, Boolean transparentMethod, StackCrawlMark & ​​stackMark) in System.Reflection. .DynamicMethod..ctor (string name, type returnType, Type [] parameterTypes, Type owner, Boolean skipVisibility) in Dapper.SqlMapper.CreateParamInfoGenerator (Identity identity, 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: line 1578 on System.Collections.Generic.List 1..ctor(IEnumerable 1 glitch nik)
in 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, transaction SqlTransaction) in C: \ Projects \ Inhouse \ ninjaevaluation \ NinjaEvaluation \ NinjaEvaluation.Data \ Database \ DapperWrapper.cs: line 52 in NinjaEvaluation.Data.Database.DapperWrapper.Invoke (Action`2 action) in C: \ Projects \ Inhouse \ ninjaevaluation \ NinjaEvaluation \ NinjaEvaluation.Data \ Database \ DapperWrapper.cs: line 68

This happens in a completely normal situation when I run my query as follows:

  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) 

I found the following SO thread started by someone with the same problem, but the problem there seemed to be in the .NET version (3.5), but I am running .NET 4.5 and I cannot figure out what the problem is.

Any suggestions?

+6
source share
2 answers

It fails because this script using Query[<T>] does not expect an array / sequence of parameters. The Execute path expects this and automatically expands the data, executing SQL once for each element, but this does not apply to Query[<T>] , so it tries to create a dynamic method associated with the array (in your case), which is prohibited. The code should probably detect this much earlier, and just say no, this is not allowed.

You probably want to change .ToArray() to .Single() .

This will become clearer after the next build; following passes:

  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"); } } 
+5
source

This does not apply to the OP case, but I encountered this error when Query was strictly typed for a collection, for example: Query<IEnumerable<string>>

When it should be just a Query<string>

+2
source

Source: https://habr.com/ru/post/987853/


All Articles