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 中注释掉时我修复了它
| 归档时间: |
|
| 查看次数: |
17047 次 |
| 最近记录: |