Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.NullReferenceException: Object reference not set to an instance of an object. #20348

Closed
ZevFung opened this issue Mar 19, 2020 · 5 comments
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@ZevFung
Copy link

ZevFung commented Mar 19, 2020

I need to delete a property in the class, and then execute add migration command, but the error is prompted

Steps to reproduce

model

using System.Collections.Generic;

namespace Domain
{
   public class Province
    {
        public Province()
        {
            Cities = new List<City>();
        }
        public int Id { get; set; }

        public string Name { get; set; }

        public string Code { get; set; }

        public List<City> Cities { get; set; }
    }
}
namespace Domain
{
    public class City
    {
        public int Id { get; set; }

        
        public string Name { get; set; }

        public string Code { get; set; }

        public string CodeName1 { get; set; }

        public int ProvinceId { get; set; }

        public Province Province { get; set; }
    }
}

I delete the CodeName1 attribute in the City class, and execute the command Add-Migration DelCodeName1 -verbose ,following error is returned.

PM> Add-Migration DelCodeName1 -verbose
Using project 'Data'.
Using startup project 'Data'.
Build started...
Build succeeded.
C:\Program Files\dotnet\dotnet.exe exec --depsfile C:\Users\pxm\source\repos\WebApplication8\Data\bin\Debug\netcoreapp3.1\Data.deps.json --additionalprobingpath C:\Users\pxm\.nuget\packages --additionalprobingpath "C:\Program Files\dotnet\sdk\NuGetFallbackFolder" --runtimeconfig C:\Users\pxm\source\repos\WebApplication8\Data\bin\Debug\netcoreapp3.1\Data.runtimeconfig.json C:\Users\pxm\.nuget\packages\microsoft.entityframeworkcore.tools\5.0.0-preview.2.20120.8\tools\netcoreapp2.0\any\ef.dll migrations add DelCodeName1 --json --verbose --no-color --prefix-output --assembly C:\Users\pxm\source\repos\WebApplication8\Data\bin\Debug\netcoreapp3.1\Data.dll --startup-assembly C:\Users\pxm\source\repos\WebApplication8\Data\bin\Debug\netcoreapp3.1\Data.dll --project-dir C:\Users\pxm\source\repos\WebApplication8\Data\ --language C# --working-dir C:\Users\pxm\source\repos\WebApplication8 --root-namespace Data
Using assembly 'Data'.
Using startup assembly 'Data'.
Using application base 'C:\Users\pxm\source\repos\WebApplication8\Data\bin\Debug\netcoreapp3.1'.
Using working directory 'C:\Users\pxm\source\repos\WebApplication8\Data'.
Using root namespace 'Data'.
Using project directory 'C:\Users\pxm\source\repos\WebApplication8\Data\'.
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider...
Finding Microsoft.Extensions.Hosting service provider...
No static method 'CreateHostBuilder(string[])' was found on class 'Program'.
No application service provider was found.
Finding DbContext classes in the project...
Found DbContext 'MyDBContext'.
Using context 'MyDBContext'.
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 'Data'.
No referenced design-time services were found.
Finding IDesignTimeServices implementations in assembly 'Data'...
No design-time services were found.
DetectChanges starting for 'MyDBContext'.
DetectChanges completed for 'MyDBContext'.
DetectChanges starting for 'MyDBContext'.
DetectChanges completed for 'MyDBContext'.
'MyDBContext' disposed.
System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Initialize(ColumnOperation columnOperation, IProperty property, CoreTypeMapping typeMapping, Boolean isNullable, IEnumerable`1 migrationsAnnotations, Boolean inline)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Add(IProperty target, DiffContext diffContext, Boolean inline)+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.Diff(ITable source, ITable 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_0.<.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.

I'm going back to version 3.1. 2.Everything is OK

Further technical details

EF Core version:5.0.0-preview.2.20120.8
Database provider: Microsoft.EntityFrameworkCore.SqlServer 5.0.0-preview.2.20120.8
Target framework: NET Core 3.1
Operating system:Windows 10
IDE: Microsoft Visual Studio Enterprise 2019 16.4.6

@ajcvickers
Copy link
Member

@bricelam @AndriySvyryd This looks like a 5.0 regression.

@AndriySvyryd
Copy link
Member

This is likely going to be fixed in #20305

@ajcvickers
Copy link
Member

Note for team: I can reproduce this on preview 1 and current daily build. I will test again after #20305 is merged.

@ajcvickers
Copy link
Member

Verified that this is fixed in the latest daily builld.

@ajcvickers ajcvickers added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug labels Mar 30, 2020
@ajcvickers ajcvickers modified the milestones: 5.0.0, 5.0.0-preview3 Mar 30, 2020
@tonyfa
Copy link

tonyfa commented Mar 31, 2020

Removed and added back in the table using Scaffold-DbContext and the error went away

@ajcvickers ajcvickers modified the milestones: 5.0.0-preview3, 5.0.0 Nov 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

No branches or pull requests

4 participants