Enable-Migrations “Object reference not set to an instance of an object”

十年热恋 提交于 2019-12-10 04:06:50

问题


I just created a new service fabric service that will periodically attempt to save some models to SQL Azure using Entity Framework and the Elastic Client Database library. I'm trying to model it as closely as I can to the SQL Azure Elastic Scale Client Library EF sample (Elastic DB Tools EF Sample). I'm at the point where I need to enable migrations, but when I run Enable-Migrations I'm getting the following error message. I'm using the ContextTypeName parameter since it sounded like it would be required if I'm going to eventually have different service fabric services targeting the same database with different models. Any suggestions?

PM> Enable-Migrations -ContextTypeName "ServiceHealth.DbService"
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.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is 
not marked as serializable."
At C:\eNowCloud\eNow.MicroServices\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.GetProjectTypes(Project project, Int32 shellVersion)
   at System.Data.Entity.Migrations.Extensions.ProjectExtensions.IsWebProject(Project project)
   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.

回答1:


I have resolved the same exception issue by specifying -StartUpProjectName parameter of Enable-Migrations command to my .NET framework dll with the db context. The issue might be with the weird startup project - problem in .IsWebProject() (asp.core in my case). See more detail answer on Enable-Migrations Exception calling "SetData" with "2" argument(s).




回答2:


If you are using visual studio 2019, when you want to add a new class library you face to kind of class library:

  1. Class Library (.NET Standard)

  2. Class Library (.NET Framework)

Well, I used to pick number1, but now I know I have made mistake.

The solution is to Pick number2.




回答3:


Instead of Enable-Migrations Do this EntityFrameworkCore\Enable-Migrations

Use EntityFrameworkCore\ before any command.

PS: Using VisualStudio 2019



来源:https://stackoverflow.com/questions/33241443/enable-migrations-object-reference-not-set-to-an-instance-of-an-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!