gpt4 book ai didi

c# - EF 生成 System.Data.SqlTypes.SqlNullValueException : Data is Null. 无法对 Null 值调用此方法或属性

转载 作者:行者123 更新时间:2023-12-01 22:59:14 30 4
gpt4 key购买 nike

我想请教一下这个查询有什么问题。

    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|ERROR|Microsoft.EntityFrameworkCore.Query|An exception occurred while iterating over the results of a query for context type 'RecipesApi.Data.AppDbContext'.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() 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()

这是数据库模型

    [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 等并使用数据库模型但结果相同......现在我有点迷路了,我认为这将是一些愚蠢的小错误,但我看不到它……这里还有我的 dbTable 模式的打印屏幕 Db Table Schema

最佳答案

我注释掉 <!-- <Nullable>enable</Nullable> -->时修复它来自.csproj

关于c# - EF 生成 System.Data.SqlTypes.SqlNullValueException : Data is Null. 无法对 Null 值调用此方法或属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72304441/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com