Dapper throws "Invalid type owner for DynamicMethod."

I ran into this error when using an interface instead of the class:

Query<MyObject> worked, while Query<IMyObject> did not


It fails because this scenario using Query[<T>] isn't expecting an array / sequence of parameters. The Execute call-path does expect this, and unrolls the data automatically, executing the SQL once per item - but this isn't the case for Query[<T>], so it tries to create the dynamic method bound to the array (in your case), which isn't allowed. The code should probably detect this much earlier, and just say "nope, that isn't allowed".

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

This will be clearer after the next build; the 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");
        }
    }

Tags:

C#

Dapper