我想问你这个问题出了什么问题。
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.SingleQueryingEnumerable
1.Enumerator.MoveNext() lambda_method334(关闭,QueryContext,DbDataReader,ResultContext,SingleQueryResultCoordinator )
以下是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; }
}
首先,我假设我的ViewModel和AutoMapper中有一些错误。因此,我跳过viewModel Automapper等,使用DB模型,但有相同的结果。现在我有点迷茫了,我想这会是个愚蠢的小错误,但我看不见.这里也是我的dbTable模式的屏幕
发布于 2022-05-20 06:34:58
当我从<!-- <Nullable>enable</Nullable> -->
中注释出.csproj时,我会修复它
发布于 2022-10-31 06:19:03
我通过将列设为notnull来解决这个问题。
并更新了数据库中为空的所有列。
在那之后它对我起了作用
谢谢
https://stackoverflow.com/questions/72304441
复制相似问题