我有一个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。
任何帮助将不胜感激。
1条答案
按热度按时间svmlkihl1#
VaccineBatchNo
是类定义中的字符串:但它在数据库中是一个
int
: