VS 2015 ASP.NET Web API (EF6) & Xamarin Enable-Mig

2019-04-03 15:22发布

问题:

I'm developing a project that will use ASP.NET Web API as the data service, and a Xamarin portable app as client.

I'm trying to enable migrations in the web app, but I get the following error:

Enable-Migrations -enableautomaticmigrations -ContextTypeName MyProject.Models.ApplicationDbContext -ProjectName MyProject -StartupProjectName MyProject.App -Verbose
Using StartUp project 'MyProject.App'.
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\weitz\.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\weitz\.nuget\packages\EntityFramework\6.1.3\tools\EntityFramework.psm1:719 char:5
+     $domain.SetData('contextProject', $contextProject)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + 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.
**PM>**

As you can see, I've tried specifying the start up project explicitly but doesn't look like the enable-migrations command is so happy about it.

It's a project I just created that uses full .NET (I'm bound to TPT/TPH model which EF Core doesn't support yet), so the EF version is 6.1.3 targeting .NET 4.6.1.

I'm on VS Community 2015 Update 3 Version 14.0.25431.01.

Update

Can't reproduce, but the issue happens even when adding a dummy start up project.
Cross posted issue here, please vote and share your experiments.

回答1:

It seems it's complaining about Using StartUp project 'MyProject.App' but you have already specified the start up project name with -StartupProjectName MyProject.App

Can you try only:

Enable-Migrations -enableautomaticmigrations -ContextTypeName MyProject.Models.ApplicationDbContext -ProjectName MyProject -StartupProjectName MyProject.App -Verbose

Make sure that in your start up project config file you have a valid connection string (unless you specify a connection string name in the DbContext constructor, your connection string should be called ApplicationDbContext, like your DbContext, if I remember correctly)


UPDATE I underestimated the problem. It seems it might be not how you specify the start up project, but the start up project itself. I suggest looking at this answer. Pay special attention, as I was saying before, that the connection string exists in the web or app.config in the start up project and has the right name.



回答2:

Well according to this (tested and works), the only way to enable migrations in aspnetcore+ef6 project, is to have the DbContext impl in an external full .NET class library, plus adding a dummy start up project.
Sucks but works.