asp.net 无法强制转换类型为“System”的对象,Int32'到类型'System,在www.example中比较两个长值时使用的字符串' www.example.com

cu6pst1q  于 2023-05-02  发布在  .NET
关注(0)|答案(1)|浏览(150)

我有一个MySQL模式,其中包含一个表Medical_Record,该表具有一个长NHSNumber值。在我的dotnet项目中,我已经创建了一个Medical_Record类,它为NHSNumber提供了一个长值,并且还成功地为它创建了一个DB Context。在我的控制器类中,我将这个值与一个参数id进行比较,它也是一个长的,但是我得到了这个错误:System.InvalidCastException:无法强制转换类型为“System”的对象。Int32'到类型'System。不知道为什么会这样,我已经检查了很多次了。

public class Medical_Record
    {
        [Key]
        public int doseNo { get; set; }
        public long NHSNumber { get; set; }
        public DateTime VaccinationDate { get; set; }
        public string? VaccineManufacturer { get; set; }
        public string? DiseaseTargeted { get; set; }
        public string? VaccineType { get; set; }
        public string? Product { get; set; }
        public string? VaccineBatchNo { get; set; }
        public string? CountryOfVaccination { get; set; }
        public string? Authority { get; set; }
        public string? Site { get; set; }
        public int TotalSeriesOfDoses { get; set; }
        public string? DisplayName { get; set; }
        public int SnomedCode { get; set; }
        public DateTime DateEntered { get; set; }
        public int ProcedureCode { get; set; }
        public sbyte Booster { get; set; }
                
        public int vaccineId { get; set; }
    }
public DbSet<Medical_Record> Medical_Record { get; set; }
[HttpGet("PatientVaccines")]
       
        [ProducesResponseType(StatusCodes.Status200OK)]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        public async Task<IActionResult> GetPatientVaccines(long id)
        {
            var medicalRecords = await _govUkDBContext.Medical_Record.Where(x => (long)x.NHSNumber == id).ToListAsync();

            if (medicalRecords == null)
            {
                return NotFound();
            }

            return Ok(medicalRecords);
        }

我的数据库是这样的

我已经尝试对这两个变量进行显式强制转换,仔细检查数据类型,但仍然收到此错误。不确定它是否来自这个,因为id是int64,NHSNumber也应该是,但是它提到了int32和string。
任何帮助将不胜感激。

svmlkihl

svmlkihl1#

VaccineBatchNo是类定义中的字符串:

public string? VaccineBatchNo { get; set; }

但它在数据库中是一个int

相关问题