dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.79k stars 3.19k forks source link

Null Reference Exception when using HasData #17851

Closed laguer71 closed 5 years ago

laguer71 commented 5 years ago

I have a quite complex project, with a separate migrations assembly, modular asp.net (orchard) project, database seeding and owned types. I was able to generate migrations with ef core 2.2, but am not longer able to do it with ef core 3. First thing I tried, after making it compile, was doing a migration, and got this stack trace

System.NullReferenceException: Object reference not set to an instance of an object. at lambda_method(Closure , ValueBuffer ) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalShadowEntityEntry..ctor(IStateManager stateManager, IEntityType entityType, ValueBuffer& valueBuffer) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryFactory.NewInternalEntityEntry(IStateManager stateManager, IEntityType entityType, Object entity, ValueBuffer& valueBuffer) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.CreateEntry(IDictionary`2 values, IEntityType entityType) at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.TrackData(IModel source, IModel target) at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Diff(IModel source, IModel target, 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.<>cDisplayClass0_0.<.ctor>b0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>cDisplayClass3_0`1.b0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) Object reference not set to an instance of an object.

The project is in this github repo

https://github.com/laguer71/Albatros

laguer71 commented 5 years ago

I issue this command in the PMC add-migration -Project Albatros.Data.Migrations -StartupProject Albatros -Context ApplicationDbContext SomeNewMigrationWithEfCore3-verbose

smitpatel commented 5 years ago

The code supplied to reproduce this issue is quite complex. It would help greatly to have a simplified project/solution or code listing that isolates the issue in EF.

laguer71 commented 5 years ago

I am trying to isolate the error, but so far have not been able to. Will keep trying latter today...

laguer71 commented 5 years ago

I havent been able to isolate the bug. @AndriySvyryd ... you tracked it to a HasData. Did you found out what entityt seed was the one ?

smitpatel commented 5 years ago

@laguer71 - No. Stacktrace tells us it is HasData but no details about what actual user code.

laguer71 commented 5 years ago

Couldn't isolate the bug, BUT found a regression bug for EF Core / Linq

VILLAN3LL3 commented 5 years ago

I'm getting the same error

Microsoft.EntityFrameworkCore.Infrastructure[10403] Entity Framework Core 3.0.0 initialized 'GamificationDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: MigrationsAssembly=Netgo.Gamification.Infrastructure using lazy-loading proxies System.NullReferenceException: Object reference not set to an instance of an object. at lambda_method(Closure , ValueBuffer ) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalShadowEntityEntry..ctor(IStateManager stateManager, IEntityType entityType, ValueBuffer& valueBuffer) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryFactory.NewInternalEntityEntry(IStateManager stateManager, IEntityType entityType, Object entity, ValueBuffer& valueBuffer) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryFactory.Create(IStateManager stateManager, IEntityType entityType, Object entity, ValueBuffer& valueBuffer) at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.CreateEntry(IDictionary2 values, IEntityType entityType) at Microsoft.EntityFrameworkCore.Update.Internal.UpdateAdapter.CreateEntry(IDictionary2 values, IEntityType entityType) at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.TrackData(IModel source, IModel target) at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Diff(IModel source, IModel target, 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.<>cDisplayClass0_0.<.ctor>b0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>cDisplayClass3_0`1.b0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) Object reference not set to an instance of an object.

I'm using Microsoft.EntityFrameworkCore 3.0.0, Microsoft.EntityFrameworkCore.Proxies 3.0.0, Microsoft.EntityFrameworkCore.SqlServer 3.0.0 targetFramework in the library project: netstandard 2.1

Here are the changes I made:

Adding a new property (type float) to two entities Renamed a property (type float) in one entity Changed a readonly property that was not mapped to a regular property in one entity

smitpatel commented 5 years ago

@JulidaRocks - Can you share a repro project?

VILLAN3LL3 commented 5 years ago

@JulidaRocks - Can you share a repro project?

@smitpatel I created a simplified project

smitpatel commented 5 years ago

@JulidaRocks - Thank you so much. I am able to reproduce the error in the repro code.

VILLAN3LL3 commented 5 years ago

@smitpatel Is there a workaround to get around this issue until your changes will be published?

smitpatel commented 5 years ago

@JulidaRocks - I am not aware of any. @AndriySvyryd ?

laguer71 commented 5 years ago

Ry adding the seed data directly to the dbset

Sent from Mailhttps://go.microsoft.com/fwlink/?LinkId=550986 for Windows 10


From: Smit Patel notifications@github.com Sent: Tuesday, October 15, 2019 12:51:37 PM To: aspnet/EntityFrameworkCore EntityFrameworkCore@noreply.github.com Cc: Luis Guerrero laguer71@hotmail.com; Mention mention@noreply.github.com Subject: Re: [aspnet/EntityFrameworkCore] Null Reference Exception when using HasData (#17851)

@JulidaRockshttps://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FJulidaRocks&data=02%7C01%7C%7C9883f18844b841c2671508d751985466%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637067586995665799&sdata=RRWkm8DymlhU90NHbmrqitOMFlT%2FTxKuJt0TLyvLKt4%3D&reserved=0 - I am not aware of any. @AndriySvyrydhttps://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FAndriySvyryd&data=02%7C01%7C%7C9883f18844b841c2671508d751985466%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637067586995675804&sdata=GryWlMnDa7xZLKjPTCuC%2BfEhZmTbqDNywQM7HkaEc%2Bc%3D&reserved=0 ?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Faspnet%2FEntityFrameworkCore%2Fissues%2F17851%3Femail_source%3Dnotifications%26email_token%3DAJSTUTJWQ4AU4QVF5RUA6MDQOX7KTA5CNFSM4IWXSHOKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEBJUXHY%23issuecomment-542329759&data=02%7C01%7C%7C9883f18844b841c2671508d751985466%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637067586995685816&sdata=KSRcuInf8Y2JFKezTRV6LpP693H9R67yF2SZcfulUvU%3D&reserved=0, or unsubscribehttps://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAJSTUTLF2KLPXF4J6HKHFBLQOX7KTANCNFSM4IWXSHOA&data=02%7C01%7C%7C9883f18844b841c2671508d751985466%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C637067586995695821&sdata=L1frbqbeaboh34NN8c4iqs5Jtvh5HVXoFiizsedFqTA%3D&reserved=0.

ciacco85 commented 5 years ago

do you think an hotfix will be released soon, like 3.0.1, or we've to wait till 3.1 in november? I thought targeting old framework for new migration could be a workaround, but only for empty migration.

ciacco85 commented 5 years ago

Ry adding the seed data directly to the dbset Sent from Mail<https://go.microsoft.com/fwlink/?

So you've moved from "HasData(new YourEntity())" to "DbSet<>().Add(new YourEntity())" for the seed that throws the exception? How did you notice wich seeded entity throws the exception, since there's no clue on how to find it? may be because you've a null reference exception instead of invalid casting exception and so you've more detail?

smitpatel commented 5 years ago

A derived entity which has seed data, where base entity has a reference navigation would throw exception in model snapshot.

ciacco85 commented 5 years ago

A derived entity which has seed data, where base entity has a reference navigation would throw exception in model snapshot.

I've debugged every single line of code, without success. As pointed in the other issue #18297 it seems that the "add migration" command read all snapshot instructions first (that I've debugged while adding migration) and, at the end, perform all his checks on data seed internally writing all those verbose logs like

'EFSDbContext' generated value '1' for the 'Discriminator' property of new 'Permission' entity. Context 'EFSDbContext' started tracking 'Permission' entity with key '{Id: d6e60f43-5518-448b-b013-52abeac1bb59}'. The 'Permission' entity with key '{Id: d6e60f43-5518-448b-b013-52abeac1bb59}' tracked by 'EFSDbContext' changed from 'Added' to 'Unchanged'. 'EFSDbContext' generated value '1' for the 'Discriminator' property of new 'Permission' entity. Context 'EFSDbContext' started tracking 'Permission' entity with key '{Id: d6e60f43-5518-448b-b013-52abeac1bb59}'. The 'Permission' entity with key '{Id: d6e60f43-5518-448b-b013-52abeac1bb59}' tracked by 'EFSDbContext' changed from 'Added' to 'Unchanged'.

and suddenly the exception is thrown without clue My even worse problem is that all models inherit from a base class with a navigation property

Any other clue?

smitpatel commented 5 years ago

perform all his checks on data seed internally writing all those verbose logs like

If you have logs then look for last entity which was tracked. With above log, it is Permission entity with given key. Whatever is next entity being used in seed data is causing the issue.

ciacco85 commented 5 years ago

perform all his checks on data seed internally writing all those verbose logs like

If you have logs then look for last entity which was tracked. With above log, it is Permission entity with given key. Whatever is next entity being used in seed data is causing the issue.

That's exactly what I've done, but without success. I will double check and see. Thank you, really appreciated. Am I wrong or your commit was merged into v.3.1.0 preview 1 recently? Maybe I can give a try with the new version, too You're doing great!!!

smitpatel commented 5 years ago

It went into v 3.1.0-preview2. Would be available in nightly builds.