具有.net核心3.1的Mysql Enterprise 8。获取错误数据为空。无法在Null值上调用此方法或属性

问题描述 投票:0回答:1

我有.net core 3.1应用程序从Linux连接时,在mysql 8企业版上出现以下错误

Exception has been thrown by the target of an invocation.
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.Data.SqlTypes.SqlNullValueException: Data is Null. This method or property cannot be called on Null values.
at MySql.Data.MySqlClient.MySqlDataReader.GetFieldValue(Int32 index, Boolean checkNull)

奇怪的是,当尝试从Windows到同一个数据库时,我没有收到此错误。我正在访问的存储过程只有一个参数。当我在连接字符串中添加CheckParameters=false时,删除了来自Linux系统的错误。但是我很困惑为什么会这样,因为我的存储过程再也没有一个参数了。

mysql-connector mysql-8.0 .net-core-3.1
1个回答
0
投票

如果您试图从数据库中读取一些可为空的数据,但是您的类型不可为空,则可能会出现此错误。

如果MyInt在数据库中可以为空,并且您具有此实体:

public class MyEntity
{
    public int Id { get; set; }
    public int MyInt { get; set; }
}

您将获得异常:System.Data.SqlTypes.SqlNullValueException:'数据为空。无法在Null值上调用此方法或属性。'

要解决此问题,只需将MyInt属性的类型更改为Nullable或int?:

public class MyEntity
{
    public int Id { get; set; }
    public int? MyInt { get; set; }
}

注意:这不是原始问题的答案,而是标题中问题的答案。

© www.soinside.com 2019 - 2024. All rights reserved.