Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EfCore 5 Update method throws "Unable to cast object of type 'System.Int32' to type 'System.Int64'." #24221

Closed
AndersonMori opened this issue Feb 22, 2021 · 4 comments
Labels
area-save-changes closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression Servicing-approved type-bug
Milestone

Comments

@AndersonMori
Copy link

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'.".

Code Example

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.

Stacktrace

 Message=Unable to cast object of type 'System.Int32' to type 'System.Int64'.
  Source=System.Private.CoreLib
  StackTrace:
   at System.Runtime.CompilerServices.CastHelpers.Unbox(Void* toTypeHnd, Object obj)
   at Microsoft.EntityFrameworkCore.ChangeTracking.ValueComparer`1.Equals(Object left, Object right)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.CompositeValueFactory.CompositeCustomComparer.Equals(Object[] x, Object[] y)
   at Microsoft.EntityFrameworkCore.Update.Internal.KeyValueIndex`1.Equals(KeyValueIndex`1 other)
   at Microsoft.EntityFrameworkCore.Update.Internal.KeyValueIndex`1.Equals(Object obj)
   at System.Collections.Generic.ObjectEqualityComparer`1.Equals(T x, T y)
   at System.Collections.Generic.Dictionary`2.FindValue(TKey key)
   at Microsoft.EntityFrameworkCore.Update.Internal.CommandBatchPreparer.AddUniqueValueEdges(Multigraph`2 commandGraph)
   at Microsoft.EntityFrameworkCore.Update.Internal.CommandBatchPreparer.TopologicalSort(IEnumerable`1 commands)
   at Microsoft.EntityFrameworkCore.Update.Internal.CommandBatchPreparer.<BatchCommands>d__11.MoveNext()
   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.<ExecuteAsync>d__9.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.<ExecuteAsync>d__9.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.<ExecuteAsync>d__9.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.<SaveChangesAsync>d__100.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.<SaveChangesAsync>d__104.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at Microsoft.EntityFrameworkCore.DbContext.<SaveChangesAsync>d__56.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.EntityFrameworkCore.DbContext.<SaveChangesAsync>d__56.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at EFCoreSaveChangesBug.Program.<Main>d__0.MoveNext() in C:\Users\anderson.mori\source\repos\EFCoreSaveChangesBug\EFCoreSaveChangesBug\Program.cs:line 138

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
Database provider: Microsoft.EntityFrameworkCore.SqlServer and Microsoft.EntityFrameworkCore.Sqlite
Target framework: NET 5.0
Operating system: Windows 10 Pro
IDE: Visual Studio 2019 16.8.4

@ajcvickers
Copy link
Member

/cc @smitpatel

@smitpatel
Copy link
Member

The stacktrace includes update pipeline and value comparer.

@AndriySvyryd
Copy link
Member

Key values between different keys are being compared in the update pipeline. I'm surprised this wasn't reported previously.

AndriySvyryd added a commit that referenced this issue Feb 23, 2021
@ajcvickers ajcvickers added this to the 5.0.5 milestone Feb 26, 2021
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Feb 26, 2021
wtgodbe pushed a commit that referenced this issue Mar 10, 2021
…tities (#24244)

* Don't compare values from different keys in for deleted entities

Fixes #24221

* Add quirk mode for #24221
@AndriySvyryd AndriySvyryd removed their assignment Mar 11, 2021
@TAGC
Copy link

TAGC commented Apr 26, 2021

Goddamn.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-save-changes closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression Servicing-approved type-bug
Projects
None yet
Development

No branches or pull requests

5 participants