EF Core 2.1项目Add-Migration NullReferenceException [英] EF Core 2.1 project Add-Migration NullReferenceException

查看:104
本文介绍了EF Core 2.1项目Add-Migration NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在最近转换为dotnet core 2.1的项目中得到了 NullReferenceException

I am getting a NullReferenceException on a project recently converted to dotnet core 2.1.

我的EF库项目是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

add-migration命令的输出

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> 

我尝试将IDesignTimeServices对象添加到启动项目中,并尝试添加IDesignTimeDbContextFactory。
我还尝试用相同的dotnet和EF版本号创建一个新的示例项目,指向相同的数据库,并且能够生成上下文并成功创建迁移。因此,我看不到的两个项目之间一定有一些区别。

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.

更新:

我最初发布的消息是此错误是由于项目迁移到标准2.0而引起的,但在将其转换回 dotnetcore2.1 并且错误仍然存​​在。

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.

推荐答案

类似的原因是通过对ModelSnapshot类中不再存在的模型的某些引用。包括我从上下文中删除 AspNetCore.Identity 之后留下的一些身份模型。

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.

删除下面的代码从我的 EscData01ContextModelSnapshot 中解决了该问题。

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);
});

这篇关于EF Core 2.1项目Add-Migration NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆