【问题标题】:EF Core RemoveRange System.InvalidCastException : Unable to cast object of type 'System.Int32' to type 'System.Int64'EF Core RemoveRange System.InvalidCastException:无法将“System.Int32”类型的对象转换为“System.Int64”类型
【发布时间】:2021-03-30 20:51:51
【问题描述】:

在一种在集成测试之间进行数据库清理的方法中,当我删除下面代码 sn-p 中的任何一个 SaveChanges() 调用时,我得到了几个测试的异常,但如果两者都存在,它工作正常。

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

堆栈跟踪是

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

我仍在试图弄清楚这到底意味着什么以及我是如何到达这里的,但是当我调试时,这就是我所看到的

            public bool Equals(object[] x, object[] y)
            {
                if (ReferenceEquals(x, y))
                {
                    return true;
                }

                if (x.Length != y.Length)
                {
                    return false;
                }

                for (var i = 0; i < x.Length; i++)
                {
                    if (!_equals[i](x[i], y[i]))
                    {
                        return false;
                    }
                }

                return true;
            }

Watch window while debugging

在堆栈行进一步挖掘(加载符号后) System.Private.CoreLib.dll!System.Collections.Generic.Dictionary>.FindValue(Microsoft.EntityFrameworkCore. Update.Internal.IKeyValueIndex 键)第 402 行

我明白了

entry = ref entries[i];
if (entry.hashCode == hashCode && defaultComparer.Equals(entry.key, key))
{
    goto ReturnFound;
}

由于某种原因,entry.key._keyValues 是 object[],一个 int 和一个 long,而 key._keyValues 是 object[],两个 long。

我已经查看了数据模型和迁移,但找不到任何从 long、long 到 int、long 或反之亦然的东西,这可能会使事情变得不一致,但我可能错过了一些关于如何处理的指针也欢迎检查。

【问题讨论】:

    标签: c# entity-framework .net-core


    【解决方案1】:

    根据我在 Github [1] 上的报告,这看起来是 EF Core 5.0.0-5.0.4 中的一个错误,应该在 5.0.5 中修复

    [1]https://github.com/dotnet/efcore/issues/24549

    【讨论】:

      猜你喜欢
      • 2017-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-07
      • 2021-10-25
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      相关资源
      最近更新 更多