在System.Reflection.Emit.DynamicILGenerator.Emit使用Dapper的参数为null异常 [英] Argument null exception using Dapper at System.Reflection.Emit.DynamicILGenerator.Emit

查看:129
本文介绍了在System.Reflection.Emit.DynamicILGenerator.Emit使用Dapper的参数为null异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Dapper 1.50.2连接到MySQL数据库,并在此方法中发出非常奇怪的错误

I am using Dapper 1.50.2 to connect to MySQL database and issued very strange error in this method

public new IEnumerable<T> Query<T>(string sql, object param = null, bool buffered = true)
{
    IEnumerable<T> result = null;
    using (var connection = Database.GetConnection())
    {
        connection.Open();
        result = connection.Query<T>(sql, param, null, buffered, 500, null);
        connection.Close();
    }      
    return result.Where(x => EntityValidator.Validate(x));
}

其中T是OldOrderConfirmation

where T was OldOrderConfirmation

public class OldOrderConfirmation : OldBaseEntity
{
    [MapToColumn("order_id")]
    public override int Id { get; set; }

    [MapToColumn("order_number")]
    public string Number { get; set; }

    [MapToColumn("user_id")]
    public int AddressId { get; set; }

    [MapToColumn("currency_code")]
    public string CurrencyCode { get; set; }

    [MapToColumn("user_po")]
    public string Name { get; set; }

    [MapToColumn("discount_value")]
    public decimal TotalDiscount { get; set; }

    [MapToColumn("order_shipping")]
    public decimal DeliveryCosts { get; set; }

    [MapToColumn("shipping_tax")]
    public decimal VATShipping { get; set; }

    [MapToColumn("order_payment")]
    public decimal Reduction { get; set; }

    [MapToColumn("payment_tax")]
    public decimal VATReduction { get; set; }

    [MapToColumn("shipping_method_id")]
    public int ShippingMethodId { get; set; }

    [MapToColumn("bemerkungen")]
    public string Comments { get; set; }

    [MapToColumn("order_date")]
    public DateTime Date { get; set; }

    [MapToColumn("adminstatus")]
    public int Status { get; set; }

    [MapToColumn("deliverydate")]
    public DateTime? DeliveryDate { get; set; }

    [MapToColumn("license_client")]
    public int Licensee { get; set; }
}

在具有以下堆栈跟踪的connection.Query方法中发生错误(ArgumentNullException:meth为null)

Error (ArgumentNullException: meth is null) occured at connection.Query method with the following stack trace:

at System.Reflection.Emit.DynamicILGenerator.Emit(OpCode opcode, MethodInfo meth)
at Dapper.SqlMapper.GetTypeDeserializerImpl(Type type, IDataReader reader, Int32 startBound, Int32 length, Boolean returnNullIfFirstMissing)
at Dapper.SqlMapper.TypeDeserializerCache.GetReader(IDataReader reader, Int32 startBound, Int32 length, Boolean returnNullIfFirstMissing)
at Dapper.SqlMapper.TypeDeserializerCache.GetReader(Type type, IDataReader reader, Int32 startBound, Int32 length, Boolean returnNullIfFirstMissing)
at Dapper.SqlMapper.GetTypeDeserializer(Type type, IDataReader reader, Int32 startBound, Int32 length, Boolean returnNullIfFirstMissing)
at Dapper.SqlMapper.GetDeserializer(Type type, IDataReader reader, Int32 startBound, Int32 length, Boolean returnNullIfFirstMissing)
at Dapper.SqlMapper.<QueryImpl>d__125`1.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at 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)

* No need in next lines *

经过不同的修复尝试(升级和降级Dapper,更改目标框架,清理/重建,重新启动Visual Studio)之后,我发现从模型中删除(甚至重命名)一个特定字段可以解决该错误.

After different attempts to fix it (upgrade and downgrade Dapper, changing target framework, clean/rebuild, relaunch Visual studio), I figured out that removing (and even renaming) one specific field from model fixed the error.

[MapToColumn("order_date")]
public DateTime Date { get; set; }

也许是由于在MySQL中保留了日期"字样或类似的东西而引起的问题,但是我让这段代码在另一个MySQL数据库上工作了!

Maybe problem could occur due to 'Date' word as reserved in MySQL or something like this, but I had this code working on another MySQL database!

其他信息:"order_date"列的类型为DATETIME

Additional info: 'order_date' column is of type DATETIME

所以我要求解释为什么会发生此错误,或者至少可能会发生此错误.

So I ask for some explanation why this error occured or at least could occur.

推荐答案

我在2021年遇到了相同的错误,但关于 meth ArgumentNullException 仍然相同.我发现的唯一答案是有关一个名为 Date 的属性,而我却没有.

I got the same error in 2021, with still the same ArgumentNullException about meth. The only answers I found were about a property named Date, which I didn't have.

我的错误是试图反序列化为仅具有参数和不可设置属性的构造函数的类(该类是从其他位置复制的).

My mistake was trying to deserialize into a class with only a constructor with parameters and non-settable properties (the class was copied from somewhere else).

public class Foo {
    public Foo(string bar) { this.Bar = bar; }
    public string Bar { get; private set; }
}

解决方案就是简单地更改为无参数构造函数,并在属性中添加公共设置器.

The solution was simply to change to a parameterless constructor and add public setters to the properties.

public class Foo {
    public Foo() { }
    public string Bar { get; set; }
}

这篇关于在System.Reflection.Emit.DynamicILGenerator.Emit使用Dapper的参数为null异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆