I get this error in an ABP project:
InvalidCastException: Unable to cast object of type 'System.Func'3[System.Nullable'1[System.DateTime],System.Nullable'1[System.DateTime],System.Boolean]' to type 'System.Func'3[System.DateTime,System.DateTime,System.Boolean]'.
I get this when i try to insert an Entity into the DB.
The Entity in question is this:
using Abp.Domain.Entities;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OperaABP.ReportKPI
{
[Table ("Semprini_Dati")]
public class Semprini_Dati : Entity<DateTime>
{
[Key]
[Required]
[Column ("Data")]
[DataType (DataType.Date)]
public override DateTime Id { get; set; }
[Required]
[Column("Imballati (pz)")]
public double Imballati_pz { get; set; }
[Required]
[Column("Lavorate (h)")]
public double Lavorate_h { get; set; }
[Required]
[Column("Prima Linea (h)")]
public double Prima_Linea_h { get; set; }
[Required]
[Column("Seconda Linea o altro (h)")]
public double Seconda_Linea_o_altro_h { get; set; }
[Required]
[Column("Produttività lorda (pz/h)")]
public double Produttivita_lorda_pz_h { get; set; }
}
}
The builder in the context is this:
modelBuilder.Entity<Semprini_Dati>(entity =>
{
entity.HasKey(e => new { e.Id}).HasName("PK_Semprini_Dati");
});
The code-block giving this error is this, and it is a method into the Repository:
public async Task<Semprini_Dati> AddAsync(Semprini_Dati dati)
{
try
{
await _context.GetDbContext().AddAsync(dati);
return dati;
}
catch (Exception)
{
throw;
}
}
An example of an inserted entity, in json, is this:
{
"data": "2024-05-23",
"imballati_pz": 260,
"lavorate_h" = 8,
"prima_Linea_h": 8,
"produttivita_lorda_pz_h" = 32.5,
"seconda_Linea_o_altro_h" = 0
}
The error seems to be due to a casting from nullable data to Non-nullable data, but none of the inserted data turns out to be nullable, so I don’t understand where the problem comes from
I tried debugging to the highest level possible, without finding anything that apparently could clarify the problem.
I double-checked that the data type was consistent with the database.
The problem persists and seems to be due to this Casting from nullable data to NON-nullable data, without having inserted any nullable data.