首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >EF生成System.Data.SqlTypes.SqlNullValueException:数据为空。不能对空值调用此方法或属性。

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

Stack Overflow用户
提问于 2022-05-19 12:21:27
回答 2查看 2.5K关注 0票数 2

我想问你这个问题出了什么问题。

代码语言:javascript
运行
复制
    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时,它可以工作。

但是在我的应用程序中

在迭代上下文类型'RecipesApi.Data.AppDbContext‘的查询结果时,出现了13:38:39.3178|10100|ERROR|Microsoft.EntityFrameworkCore.Query|An异常2022-05-19。System.Data.SqlTypes.SqlNullValueException:数据是空的。不能对空值调用此方法或属性。在Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable1.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() lambda_method334(关闭,QueryContext,DbDataReader,ResultContext,SingleQueryResultCoordinator )

以下是Db模型

代码语言:javascript
运行
复制
    [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模型,但有相同的结果。现在我有点迷茫了,我想这会是个愚蠢的小错误,但我看不见.这里也是我的dbTable模式的屏幕

EN

回答 2

Stack Overflow用户

发布于 2022-05-20 06:34:58

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

票数 3
EN

Stack Overflow用户

发布于 2022-10-31 06:19:03

我通过将列设为notnull来解决这个问题。

并更新了数据库中为空的所有列。

在那之后它对我起了作用

谢谢

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72304441

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档