EfCore 5 Update method throws "Unable to cast object of type 'System.Int32' to type 'System.Int64'." #24221
After upgrading from EFcore 3.1 to EFcore 5.0.3, some manipulations of Entities started to throw "Unable to cast object of type 'System.Int32' to type 'System.Int64'.".
On the following Code, the line of "SaveChangesAsync()" throw "Unable to cast object of type 'System.Int32' to type 'System.Int64'.", but works fine in EFcore 3.1 full example :
var schoolDB = context.Set<School>(); var schoolEdit = schoolDB .Include(c => c.SchoolSubject) .Include(c => c.SchoolSubjectTeacher) .ThenInclude(c => c.SchoolSubjectTeacherFloor) .First(); schoolEdit.SchoolSubject!.RemoveAll(a => true); schoolEdit.SchoolSubjectTeacher!.RemoveAll(a => true); context.Update(schoolEdit); await context.SaveChangesAsync();
Note that only values/rows were Deleted.
Sorry for the Sample, I did my best converting my real project to an understandable Code.
Provider and version information
EF Core version: 5.0.3
The text was updated successfully, but these errors were encountered:
Ive spent a whole day or so trying to tackle this issue in my own project, attached the debugger during migration generation and turned off Just My Code, dug right into the core of the issue, finally understood what was happening and created an SSCCE that minimally reproduced this issue.
Then just as I was typing up the bug report I realised that I was targeting EF Core 5.0.4 instead of 5.0.5. I didn't think it would make any difference but bumped my SSCCE up to the latest version and tried reproducing it again...and there was no bug. I can't believe that this entire time I could have avoided all that work if I'd just noticed I was one tiny patch version behind.