I am getting a NullReferenceException
on a project recently converted to dotnet core 2.1.
My EF library project is dotnet core 2.1. Code first migrations.
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
Output from the add-migration command
PM> 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.<>c.<GetRootType>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.<>c__DisplayClass0_1.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Object reference not set to an instance of an object.
PM>
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.
PM> 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.
Any help greatly appreciated.
Update:
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 dotnetcore2.1
and error persisted.
Open the Package Manager Console from Tools → Library Package Manager → Package Manager Console and then run the enable-migrations command (make sure that the default project is the project where your context class is).
Using multiple context types One way to create multiple migration sets is to use one DbContext type per provider. Specify the context type when adding new migrations. You don't need to specify the output directory for subsequent migrations since they are created as siblings to the last one.
Looks like this error was caused by some references to models in my ModelSnapshot class that did not exist anymore. Including some Identity models that were left behind after I removed AspNetCore.Identity
from the Context.
Deleting the code below from my EscData01ContextModelSnapshot
fixed the issue.
modelBuilder.Entity("Sms.Office.Data.EscData01.Models.ExcelTemplate", b =>
{
b.HasOne("Sms.Office.Data.EscData01.Models.File", "File")
.WithMany()
.HasForeignKey("FileId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b =>
{
b.HasOne("Sms.Office.Data.EscData01.Authentication.ApplicationRole")
.WithMany()
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<string>", b =>
{
b.HasOne("Sms.Office.Data.EscData01.Authentication.ApplicationUser")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<string>", b =>
{
b.HasOne("Sms.Office.Data.EscData01.Authentication.ApplicationUser")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<string>", b =>
{
b.HasOne("Sms.Office.Data.EscData01.Authentication.ApplicationRole")
.WithMany()
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Cascade);
b.HasOne("Sms.Office.Data.EscData01.Authentication.ApplicationUser")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<string>", b =>
{
b.HasOne("Sms.Office.Data.EscData01.Authentication.ApplicationUser")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With