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

EF Core RemoveRange System.InvalidCastException : Unable to cast object of type 'System.Int32' to type 'System.Int64' #24549

Closed
jons-aura opened this issue Mar 30, 2021 · 4 comments

Comments

@jons-aura
Copy link

Include your code

I'm having trouble creating a small reproducible test case. It doesn't help that it seems to be dependent on something at runtime as when I run all the of integration tests at once everything passes but if I run a specific subset two of them fail.

In a method to do database cleanup between integration tests I get exceptions for a couple tests when I remove either of the SaveChanges() calls in the code snippet below but if both are present it works fine.

System.InvalidCastException : Unable to cast object of type 'System.Int32' to type 'System.Int64'.
... A bunch of RemoveRange()
           ctx.RemoveRange(ctx.RoleXPermission);
            ctx.SaveChanges();
            ctx.RemoveRange(ctx.UsersXAuthorizationGroup);
            ctx.SaveChanges();
            ctx.RemoveRange(ctx.AuthorizationGroupXRole);
... A couple more RemoveRange()

Include stack traces

ValueComparer`1.Equals(Object left, Object right)
    CompositeCustomComparer.Equals(Object[] x, Object[] y)
    KeyValueIndex`1.Equals(KeyValueIndex`1 other)
    KeyValueIndex`1.Equals(Object obj)
    Dictionary`2.FindValue(TKey key)
    CommandBatchPreparer.AddUniqueValueEdges(Multigraph`2 commandGraph)
    CommandBatchPreparer.TopologicalSort(IEnumerable`1 commands)
    CommandBatchPreparer.BatchCommands(IList`1 entries, IUpdateAdapter updateAdapter)+MoveNext()
    BatchExecutor.Execute(IEnumerable`1 commandBatches, IRelationalConnection connection)
    RelationalDatabase.SaveChanges(IList`1 entries)
    StateManager.SaveChanges(IList`1 entriesToSave)
    StateManager.SaveChanges(DbContext _, Boolean acceptAllChangesOnSuccess)
    SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
    StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
    DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
    DbContext.SaveChanges()
    TestBase.GetCleanedTestContext() line 235
    TestBase.SetUp() line 48

Include provider and version information

EF Core version: 5.0.4
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: netstandard2.1
Operating system: Windows 10
IDE: Visual Studio 2019 16.9.2

I've also asked a question on StackOverflow here https://stackoverflow.com/questions/66878339/ef-core-removerange-system-invalidcastexception-unable-to-cast-object-of-type

@smitpatel
Copy link
Member

Possible duplicate of #24221

@jons-aura
Copy link
Author

@smitpatel That looks like exactly what I was seeing. What's the planned release date for 5.0.5?

@ajcvickers
Copy link
Member

@jons-aura 5.0.5 is currently going through release validation and should be available soon.

@triumphtang
Copy link

triumphtang commented Aug 19, 2021

MySQL8.0.26 and 5.0.9 Execute “client.Database.EnsureCreatedAsync().Wait();” got same error. "One or more errors occurred. (Unable to cast object of type 'System.Int64' to type 'System.Int32'.)"

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants