EF 6 - 使用 2 个参数调用 SetData 时出现错误异常

问题描述 投票:0回答:1

我有一个包含多个项目的解决方案。 DBContext 文件和模型位于 ClassLibrary 项目中。该项目具有 EF 6.1.3,并且在 .NET 4.5.2 上运行。模型和 DBContext 文件具有不同的文件结构,我的意思是模型位于 Project/Data/Model 中,DBContext 位于 Data 文件夹中。该项目似乎已启用到现有数据库的迁移。开发团队过去只是通过构建项目来重新创建数据库。它还具有:

AutomaticMigrationsEnabled = true;

我正在尝试正确使用迁移,并尝试按照以下方式查看 PackageManagerConsole 的正确反应。

Enable-Migrations
Add-Migration InitialCreate –IgnoreChanges

两者都导致以下错误:

Exception calling "SetData" with "2" argument(s): "Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject' in assembly  'Microsoft.VisualStudio.ProjectSystem.VS.Implementation, Version=14.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable." At C:\Users\Temp\.nuget\packages\EntityFramework\6.1.3\tools\EntityFramework.psm1:718 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=14.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable." At C:\Users\Temp\.nuget\packages\EntityFramework\6.1.3\tools\EntityFramework.psm1:719 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=14.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable." At C:\Users\Temp\.nuget\packages\EntityFramework\6.1.3\tools\EntityFramework.psm1:720 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.EnableMigrationsCommand.FindContextToEnable(String contextTypeName)    at System.Data.Entity.Migrations.EnableMigrationsCommand.<>c__DisplayClass2.<.ctor>b__0() at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command) Object reference not set to an instance of an object.

您能否检查一下并告诉我导致错误的原因。我还尝试将启动项目设置为解决方案中的其他项目。

c# database entity-framework-6 entity-framework-migrations
1个回答
0
投票

晚上好,

我相信我已经发现了你的错误。根据提供的信息,它围绕实体框架迁移期间特定类型的序列化。该错误消息表明类型 (Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject) 未标记为可序列化,这会在迁移过程中导致问题。

为了解决这个问题,我建议:

  1. 检查兼容性以验证您使用的实体框架版本是否与您拥有的 Visual Studio 版本兼容。有时,某些版本的 EF 可能与特定版本的 Visual Studio 存在问题。

  1. 在配置中显式设置ContextAssemblyName。在 Configuration.cs 文件(在迁移设置期间创建)中,您可以尝试显式设置 ContextAssemblyName。打开 Migrations 文件夹中的 Configuration.cs 文件,并在 Configuration 类构造函数中添加以下行: ContextAssemblyName = "你的名字在这里";
© www.soinside.com 2019 - 2024. All rights reserved.