簡體   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