簡體   English   中英

EF Core RemoveRange System.InvalidCastException:無法將“System.Int32”類型的 object 轉換為“System.Int64”類型

[英]EF Core RemoveRange System.InvalidCastException : Unable to cast object of type 'System.Int32' to type 'System.Int64'

在一種在集成測試之間進行數據庫清理的方法中,當我刪除下面代碼片段中的任何一個 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;
            }

調試時觀看 window

在堆棧行進一步挖掘(加載符號后)System.Private.CoreLib.dll.System.Collections.Generic.Dictionary<Microsoft.EntityFrameworkCore.Update.Internal,IKeyValueIndex。 System.Collections.Generic.List<Microsoft.EntityFrameworkCore.Update.ModificationCommand>>.FindValue(Microsoft.EntityFrameworkCore.Update.Internal.IKeyValueIndex key) 第 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 。

我查看了數據 model 和遷移,找不到任何東西從 long、long 到 int、long 或反之亦然,這可能會使事情變得不一致,但我可能錯過了一些關於如何檢查的指示也歡迎。

根據我關於 Github [1] 的報告,這看起來是 EF Core 5.0.0-5.0.4 中的一個錯誤,應該在 5.0.5 中修復

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM