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

Luk*_*nek 6 c# automapper entity-framework-core

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

    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();
            }
        }
Run Code Online (Sandbox Code Playgroud)

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

但在我的应用程序中生成

2022-05-19 13:38:39.3178|10100|错误|Microsoft.EntityFrameworkCore.Query|迭代上下文类型“RecipesApi.Data.AppDbContext”的查询结果时发生异常。System.Data.SqlTypes.SqlNullValueException:数据为空。不能对 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.SingleQueryingEnumerable1.Enumerator.MoveNext()

这是 Db 模型

    [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; }
    }
Run Code Online (Sandbox Code Playgroud)

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

Luk*_*nek 13

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

  • 在这里,EF POCO 模型成员及其各自的数据库字段的可空性(“[必需]”)的不匹配为零。因此,这个漂亮的小修复消除了我们的“SqlNullValueException”,这一事实让我有点担心。为什么.NET 5/6/7 首先引入这个问题?通过删除“&lt;Nullable&gt;enable...”我还失去了什么(读:“中断”)?我需要在每个依赖的 .csproj 中执行相同的操作吗?...即使该项目没有引用 EF 或 POCO? (2认同)