提问人:steveoshima 提问时间:8/23/2023 最后编辑:steveoshima 更新时间:8/23/2023 访问量:53
Entity Framework Core 从 2.1.4 升级到 5.0.5 或 6.0.* 迁移添加错误 无法将类型为“System.Int32”的对象强制转换为类型“System.Int64”
Entity Framework Core upgrade from 2.1.4 to 5.0.5 or 6.0.* migration add error Unable to cast object of type 'System.Int32' to type 'System.Int64'
问:
已确认在从 ef core 2.1.4 升级到最新版本 5 或 6 之前,我没有进行任何数据库更改或模型更改。然而,我现在似乎在添加迁移过程中遇到了这个错误。
Unable to cast object of type 'System.Int32' to type 'System.Int64'.
我使用命令
dotnet ef migrations -v add ChangeNameExample -o Models/Db/Migrations
我检查了所有数据库表列都与模型匹配,即 bigint sql 列是模型中的长类型。我主要查看的文件是 DbContext 类。使用 ef 迁移更改后,我看不到其他位置。
回顾了重大更改升级说明,没有关于将 int 转换为长问题的任何内容。
将尝试一个新项目,看看是否需要更改任何 modelBuilder 属性。
还有其他人遇到过这个吗?
编辑:
在 OnConfiguration() 中使用这些设置进行全栈跟踪。
optionsBuilder.EnableSensitiveDataLogging();
optionsBuilder.EnableDetailedErrors();
System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.Int64'.
at lambda_method34(Closure , IUpdateEntry )
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.SimplePrincipalKeyValueFactory`1.CreateFromCurrentValues(IUpdateEntry entry)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap`1.Add(InternalEntityEntry entry)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.StartTracking(InternalEntityEntry entry)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState oldState, EntityState newState, Boolean acceptChanges, Boolean modifyProperties)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState entityState, Boolean acceptChanges, Boolean modifyProperties, Nullable`1 forceStateWhenUnknownKey)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.Microsoft.EntityFrameworkCore.Update.IUpdateEntry.set_EntityState(EntityState value)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.TrackData(IRelationalModel source, IRelationalModel target, DiffContext diffContext)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetDataOperations(IRelationalModel source, IRelationalModel target, DiffContext diffContext)+MoveNext()
at System.Linq.Enumerable.ConcatIterator`1.MoveNext()
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Sort(IEnumerable`1 operations, DiffContext diffContext)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetDifferences(IRelationalModel source, IRelationalModel target)
at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace, String language)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Unable to cast object of type 'System.Int32' to type 'System.Int64'.
答: 暂无答案
评论
Exception.ToString()
Copy Details