18

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.

Community
  • 1
  • 1
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
  • Does your startup project contains the app.config or web.config you use to connect to database? You can also use the project containing the dbcontext as a startup project. – jomsk1e Feb 16 '17 at 19:07
  • It uses appsettings.json to store connection string. But I tried adding the connection string to app.config and it didn't help. – Shimmy Weitzhandler Feb 17 '17 at 03:26

2 Answers2

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.

Community
  • 1
  • 1
Francesc Castells
  • 2,692
  • 21
  • 25
  • 1
    Comment on update: I verified all the connection strings were set well, and the start up project (the project itself) has web.config set well too. I believe the only option is adding a dummy console app and set it as startup. – Shimmy Weitzhandler Feb 19 '17 at 00:04
  • Awarded 25 bounty for the effort, anyway the answers don't work. I've posted an [issue](https://github.com/aspnet/EntityFramework6/issues/317) on the EF6 repo, please comment on. – Shimmy Weitzhandler Jul 14 '17 at 03:24
1

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.

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632