EF 6 - 使用2个参数调用SetData的错误异常

ali*_*ohn 10 c# database ef-migrations entity-framework-6

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

AutomaticMigrationsEnabled = true;
Run Code Online (Sandbox Code Playgroud)

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

Enable-Migrations
Add-Migration InitialCreate –IgnoreChanges
Run Code Online (Sandbox Code Playgroud)

两者都导致以下错误:

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.
Run Code Online (Sandbox Code Playgroud)

能否请您查看并告诉我导致错误的原因.我也尝试将Startup项目设置为解决方案中的其他项目.

小智 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 = "YourNameHere";