title: EF Core 2.1 project Add-Migration NullReferenceException
body: <p>I am getting a <code>NullReferenceException</code> on a project recently converted to dotnet core 2.1.</p>
  
  <p>My EF library project is dotnet core 2.1. Code first migrations.</p>
  
  <pre><code>CsvHelper 6.0.2
  Microsoft.EntityFrameworkCore 2.1.0
  Microsoft.EntityFrameworkCore.Design 2.1.0
  Microsoft.EntityFrameworkCore.SqlServer 2.1.0
  Microsoft.EntityFrameworkCore.Tools 2.1.0
  </code></pre>
  
  <p>Output from the add-migration command</p>
  
  <pre><code>PM&gt; add-migration -Name MyMigration -Context EscData01Context -Project Data\EscData01 -StartupProject PacmsTri -verbose
  Using project 'Data\EscData01'.
  Using startup project 'PacmsTri'.
  Build started...
  Build succeeded.
  C:\Program Files\dotnet\dotnet.exe exec --depsfile C:\temp\PacmsTri\dev\***\source\PacmsTri\bin\Debug\netcoreapp2.1\PacmsTri.deps.json --additionalprobingpath "C:\Users\***\.nuget\packages" --additionalprobingpath "C:\Program Files\dotnet\sdk\NuGetFallbackFolder" --runtimeconfig C:\temp\PacmsTri\dev\***\source\PacmsTri\bin\Debug\netcoreapp2.1\PacmsTri.runtimeconfig.json "C:\Users\***\.nuget\packages\microsoft.entityframeworkcore.tools\2.1.0\tools\netcoreapp2.0\any\ef.dll" migrations add MyMigration --json --context EscData01Context --verbose --no-color --prefix-output --assembly C:\temp\PacmsTri\dev\****\source\PacmsTri\bin\Debug\netcoreapp2.1\EscData01.dll --startup-assembly C:\temp\PacmsTri\dev\****\source\PacmsTri\bin\Debug\netcoreapp2.1\PacmsTri.dll --project-dir C:\temp\PacmsTri\dev\****\source\Data\EscData01\ --language C# --working-dir C:\temp\PacmsTri\dev\****\source --root-namespace Sms.Office.Data.EscData01
  Using assembly 'EscData01'.
  Using startup assembly 'PacmsTri'.
  Using application base 'C:\temp\PacmsTri\dev\***\source\PacmsTri\bin\Debug\netcoreapp2.1'.
  Using working directory 'C:\temp\PacmsTri\dev\***\source\PacmsTri'.
  Using root namespace 'Sms.Office.Data.EscData01'.
  Using project directory 'C:\temp\PacmsTri\dev\***\source\Data\EscData01\'.
  Finding DbContext classes...
  Finding IDesignTimeDbContextFactory implementations...
  Finding application service provider...
  Finding IWebHost accessor...
  Using environment 'Development'.
  Using application service provider from IWebHost accessor on 'Program'.
  Found DbContext 'AuthDbContext'.
  Found DbContext 'DmsContext'.
  Found DbContext 'EscData01Context'.
  Found DbContext 'MailServicesContext'.
  Found DbContext 'PmsDdContext'.
  Finding DbContext classes in the project...
  Using context 'EscData01Context'.
  Microsoft.EntityFrameworkCore.Infrastructure[10403]
        Entity Framework Core 2.1.0-rtm-30799 initialized 'EscData01Context' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
  Finding design-time services for provider 'Microsoft.EntityFrameworkCore.SqlServer'...
  Using design-time services from provider 'Microsoft.EntityFrameworkCore.SqlServer'.
  Finding design-time services referenced by assembly 'PacmsTri'.
  No referenced design-time services were found.
  Finding IDesignTimeServices implementations in assembly 'PacmsTri'...
  No design-time services were found.
  System.NullReferenceException: Object reference not set to an instance of an object.
     at Microsoft.EntityFrameworkCore.Metadata.Internal.TableMapping.&lt;&gt;c.&lt;GetRootType&gt;b__10_0(IEntityType t)
     at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
     at Microsoft.EntityFrameworkCore.Metadata.Internal.TableMapping.GetRootType()
     at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetSortedProperties(TableMapping target)
     at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Add(TableMapping target, DiffContext diffContext)+MoveNext()
     at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.DiffCollection[T](IEnumerable`1 sources, IEnumerable`1 targets, DiffContext diffContext, Func`4 diff, Func`3 add, Func`3 remove, Func`4[] predicates)+MoveNext()
     at System.Linq.Enumerable.ConcatIterator`1.MoveNext()
     at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Sort(IEnumerable`1 operations, DiffContext diffContext)
     at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetDifferences(IModel source, IModel target)
     at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace, String language)
     at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
     at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
     at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.&lt;&gt;c__DisplayClass0_1.&lt;.ctor&gt;b__0()
     at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.&lt;&gt;c__DisplayClass3_0`1.&lt;Execute&gt;b__0()
     at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
  Object reference not set to an instance of an object.
  PM&gt; 
  </code></pre>
  
  <p>I have tried adding a IDesignTimeServices object to my startup project, and tried adding a IDesignTimeDbContextFactory as well. 
  I also tried creating a new sample project with the same dotnet and EF versions numbers, pointing at same db and was able to generate the context and create the migration successfully. So there must be some difference between the two project I cannot see.</p>
  
  <pre><code>PM&gt; add-migration MyMigration2 -Project ClassLibrary1 -StartupProject WebApplication20 -Context ESC_DATA01Context
  Microsoft.EntityFrameworkCore.Infrastructure[10403]
        Entity Framework Core 2.1.0-rtm-30799 initialized 'ESC_DATA01Context' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
  To undo this action, use Remove-Migration.
  </code></pre>
  
  <p>Any help greatly appreciated.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I had originally posted that this error was caused by migration of the project to standard 2.0 but discounted that idea after converting it back to <code>dotnetcore2.1</code> and error persisted.</p>