我正在使用Dapper 1.50.2连接到MySQL数据库并在此方法中发出了非常奇怪的错误
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
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):
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),我发现从模型中删除(甚至重命名)一个特定字段修复了错误。
[MapToColumn("order_date")]
public DateTime Date { get; set; }
由于MySQL中保留的“日期”字或类似的东西,可能会出现问题,但我将此代码用于另一个MySQL数据库!
其他信息:'order_date'列的类型为DATETIME
所以我要求解释为什么会发生这种错误或者至少可能发生错误。
答案 0 :(得分:-1)
我在 2021 年遇到了同样的错误,关于 ArgumentNullException
的 meth
仍然相同。我找到的唯一答案是关于名为 Date
的属性,我没有。
我的错误是试图反序列化为一个只有带有参数和不可设置属性的构造函数的类(该类是从其他地方复制的)。
public class Foo {
public Foo(string bar) { this.Bar = bar; }
public string Bar { get; private set; }
}
解决方案只是更改为无参数构造函数并向属性添加公共设置器。
public class Foo {
public Foo() { }
public string Bar { get; set; }
}