I would like to ask you what is wrong in this query.
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();
}
}
when I use .QueryString() and copy to AzureDataStudio it works.
but in my app generate
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.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()
Here is 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; }
}
Firstly I assume there is some error in my ViewModel and AutoMapper. So I skip viewModel Automapper etc and use DB model but have same result...
Now I'm little bit lost I think it will be some small stupid mistake but I cannot see it...
Also here printscreen of my dbTable schema
nullvalue is returned for a non-nullable property, even if it's a reference type. For example, ifRecipeNameis null, you'll get an error because the type isstring, notstring?nullis encountered.RecipeNameandCommentfields are nullable but the properties arestringinstead ofstring?