ASP.NET Web API执行Add-Migration InitialCreate时遇序列化及空引用错误
EF6迁移报错:OAProject类型未标记为可序列化
问题场景
在ASP.NET Web API项目中执行Add-Migration InitialCreate命令添加Entity Framework迁移时,出现序列化相关错误,数据库连接正常。
错误信息
PM> Add-Migration InitialCreate Exception calling "SetData" with "2" argument(s): "Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject' in assembly 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation, Version=17.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable." At C:\Users\Ctop\.nuget\packages\entityframework\6.0.0\tools\EntityFramework.psm1:609 char:5 + $domain.SetData('project', $project) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : SerializationException Exception calling "SetData" with "2" argument(s): "Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject' in assembly 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation, Version=17.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable." At C:\Users\Ctop\.nuget\packages\entityframework\6.0.0\tools\EntityFramework.psm1:610 char:5 + $domain.SetData('contextProject', $contextProject) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : SerializationException Exception calling "SetData" with "2" argument(s): "Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject' in assembly 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation, Version=17.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable." At C:\Users\Ctop\.nuget\packages\entityframework\6.0.0\tools\EntityFramework.psm1:611 char:5 + $domain.SetData('startUpProject', $startUpProject) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : SerializationException System.NullReferenceException: Object reference not set to an instance of an object. at System.Data.Entity.Migrations.Extensions.ProjectExtensions.GetPropertyValue[T](Project project, String propertyName) at System.Data.Entity.Migrations.MigrationsDomainCommand.GetFacade(String configurationTypeName, Boolean useContextWorkingDirectory) at System.Data.Entity.Migrations.AddMigrationCommand.Execute(String name, Boolean force, Boolean ignoreChanges) at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command) Object reference not set to an instance of an object. PM>
解决方法
- 升级EntityFramework包:当前使用的EF6.0.0和VS2022(版本17.x)存在兼容性问题,将NuGet包
EntityFramework升级到6.4.4及以上稳定版本,该版本修复了VS2022下的序列化问题。 - 确认Package Manager Console设置:在控制台顶部的“默认项目”下拉菜单中选中包含DbContext的项目,同时确保启动项目设置正确。
- 改用命令行工具执行迁移:
- 打开命令提示符,切换到项目根目录
- 执行
Install-Package EntityFramework确保包安装完成 - 执行
Enable-Migrations -ContextTypeName 你的DbContext类名(若未启用迁移) - 执行
Add-Migration InitialCreate
- 清理重建项目:清理解决方案(生成->清理解决方案),然后重建所有项目,修复可能存在的引用损坏问题。
- 重置VS设置:若上述方法无效,尝试重置VS设置(工具->导入和导出设置->重置所有设置),排除IDE配置问题。
内容的提问来源于stack exchange,提问作者Ali Moussawi
相关产品推荐
相关产品推荐

