繁体   English   中英

EF 生成 System.Data.SqlTypes.SqlNullValueException: 数据为 Null。无法对 Null 值调用此方法或属性

[英]EF generate System.Data.SqlTypes.SqlNullValueException: Data is Null. This method or property cannot be called on Null values

我想问你这个查询有什么问题。

    public async Task<RecipeHeader> GetRecipeWithRecipeLineChild(int id)
        {
            try
            {
                return await dbSet.Where(x => x.RecipeHeaderId == id)
                    .Include(x => x.RecipeLines)
                    .ThenInclude(x => x.Unit)
                    .Include(x => x.CalculationMethod)
                    .ThenInclude(y => y.Calculation)
                    .FirstOrDefaultAsync();            
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "{Repo} GetRecipeWithRecipeLineChild function error", typeof(RecipeHeaderRepository));
                return new RecipeHeader();
            }
        }

当我使用.QueryString()并将其复制到 AzureDataStudio 时,它可以正常工作。

但在我的应用程序中生成

2022-05-19 13:38:39.3178|10100|错误|Microsoft.EntityFrameworkCore.Query|迭代上下文类型“RecipesApi.Data.AppDbContext”的查询结果时发生异常。 System.Data.SqlTypes.SqlNullValueException:数据为 Null。无法对 Null 值调用此方法或属性。 在 lambda_method334(Closure, QueryContext, DbDataReader, ResultContext, SingleQueryResultCoordinator ) 在 Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable 1.Enumerator.MoveNext() System.Data.SqlTypes.SqlNullValueException: Data is Null. This method or property cannot be called on Null values. at lambda_method334(Closure, QueryContext, DbDataReader, ResultContext, SingleQueryResultCoordinator ) at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable 1.Enumerator.MoveNext() System.Data.SqlTypes.SqlNullValueException: Data is Null. This method or property cannot be called on Null values. at lambda_method334(Closure, QueryContext, DbDataReader, ResultContext, SingleQueryResultCoordinator ) at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable 1.Enumerator.MoveNext()

这是 Db model

    [Index(nameof(RecipeId),IsUnique =true)]
    public class RecipeHeader
    {
        
        [Key]
        public int RecipeHeaderId { get; set; }

        [MaxLength(15)]
        public string RecipeId { get; set; }
        [MaxLength(50)]
        public string RecipeName { get; set; } = "";
        public int? CalculationMethodId { get; set; }
        [ForeignKey("CalculationMethodId")]
        public virtual CalculationMethod CalculationMethod  { get; set; }

        [MaxLength(80)]
        public string Comment { get; set; }
        public int? PrescriptionId { get; set; }

        [ForeignKey("PrescriptionId")]
        public virtual Prescription Prescription { get; set; }
        public bool ColorRecipe { get; set; }
        public byte? Salt { get; set; }
        public byte? Program { get; set; }
        public ushort Version { get; set; }
        public DateTime CreatedDate { get; set; }
        public DateTime? UpdatedDate { get; set; }
        public string UpdatedBy { get; set; } = "system";
        public bool Active { get; set; }
        public byte RecipeStatus { get; set; }= 1;
        public virtual ICollection<RecipeLine> RecipeLines { get; set; }
    }

首先,我假设我的 ViewModel 和 AutoMapper 中存在一些错误。 所以我跳过 viewModel Automapper 等并使用 DB model 但结果相同......现在我有点迷路我认为这将是一些愚蠢的小错误但我看不到它......这里还有我的 dbTable 模式的打印屏幕数据库表模式

当我从 .csproj 注释掉<!-- <Nullable>enable</Nullable> -->时,我会修复它

我通过将列设置为 notnull 解决了这个问题

并更新了数据库中 null 的所有列

之后它对我有用

谢谢

对我来说,我认为是 OP,问题是 Panagiotis Kanavos 在评论中提到的。

这是因为我返回的可为空的字段被映射到一个不可为空的字段。

当数据返回 null 值时,您会收到运行时错误。 错误消息未指定哪个字段导致错误。

将代码中的违规字段从DateTime类型更改为DateTime? 匹配数据库模式中的可空字段为我解决了它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM