【发布时间】:2021-04-20 09:59:40
【问题描述】:
我正在从 net core 3.1 迁移到 net 5 使用 ef core 的 net core 3.1 测试存储库正在通过,但升级到 net core 5 后,dbContext 上的 SaveChanges 抛出 nullreferenceException
这是堆栈跟踪
at Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter`2.<>c__DisplayClass3_0`2.<SanitizeConverter>b__0(Object v)
at Microsoft.EntityFrameworkCore.InMemory.Storage.Internal.InMemoryTable`1.SnapshotValue(IProperty property, ValueComparer comparer, IUpdateEntry entry)
at Microsoft.EntityFrameworkCore.InMemory.Storage.Internal.InMemoryTable`1.<>c__DisplayClass18_0.<Create>b__0(IProperty p)
at System.Linq.Enumerable.SelectEnumerableIterator`2.ToArray()
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at Microsoft.EntityFrameworkCore.InMemory.Storage.Internal.InMemoryTable`1.Create(IUpdateEntry entry)
at Microsoft.EntityFrameworkCore.InMemory.Storage.Internal.InMemoryStore.ExecuteTransaction(IList`1 entries, IDiagnosticsLogger`1 updateLogger)
at Microsoft.EntityFrameworkCore.InMemory.Storage.Internal.InMemoryDatabase.SaveChanges(IList`1 entries)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IList`1 entriesToSave)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(DbContext _, Boolean acceptAllChangesOnSuccess)
at Microsoft.EntityFrameworkCore.Storage.NonRetryingExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
at Microsoft.EntityFrameworkCore.DbContext.SaveChanges()
用于测试的 dbContext 是使用这些选项创建的
var options = new DbContextOptionsBuilder<DcmCockpitDbContext>()
.UseInMemoryDatabase(Guid.NewGuid().ToString())
.Options;
using (_context = new DcmCockpitDbContext(options, null))
{
SeedDatabase(_context);
}
是否有任何会引发此异常的重大更改? 我应该怎么做才能避免这种情况?
【问题讨论】:
-
这是唯一似乎适用的重大更改。你的测试中能有这样的代码吗? docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-5.0/…
标签: c# .net-core entity-framework-core xunit