question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ToView for entity types with owned types throws NRE

See original GitHub issue

Given a domain consisting of two entities X and Y where X owns an instance of Y and both are mapped to a view, any attempt made to create an initial database migration using Add-Migration or dotnet ef migration add fails with a NullReferenceException.

Steps to reproduce

  1. Create a .NET Core 3 console application (Experiment).
  2. Install Microsoft.EntityFrameworkCore.Tools in Experiment.
  3. Create a .NET Standard 2.1 C# library (Experiment.Lib).
  4. Install Microsoft.EntityFrameworkCore.SqlServer in Experiment.Lib.
  5. Create these classes in Experiment.Lib.
  6. Reference Experiment.Lib from Experiment.
  7. In Visual Studio, open the Package Manager Console and set the default project to Experiment.Lib, then run Add-Migration InitialCreate.

Actual Result

The following exception is generated:

PM> Add-Migration InitialCreate -Verbose
Using project 'Experiment.Lib'.
Using startup project 'Experiment'.
Build started...
Build succeeded.
C:\Program Files\dotnet\dotnet.exe exec --depsfile C:\Projects\Experiment\Experiment\bin\Debug\netcoreapp3.0\Experiment.deps.json --additionalprobingpath C:\Users\e0065186\.nuget\packages --additionalprobingpath "C:\Program Files\dotnet\sdk\NuGetFallbackFolder" --runtimeconfig C:\Projects\Experiment\Experiment\bin\Debug\netcoreapp3.0\Experiment.runtimeconfig.json C:\Users\e0065186\.nuget\packages\microsoft.entityframeworkcore.tools\3.0.0\tools\netcoreapp2.0\any\ef.dll migrations add InitialCreate --json --verbose --no-color --prefix-output --assembly C:\Projects\Experiment\Experiment\bin\Debug\netcoreapp3.0\Experiment.Lib.dll --startup-assembly C:\Projects\Experiment\Experiment\bin\Debug\netcoreapp3.0\Experiment.dll --project-dir C:\Projects\Experiment\Experiment.Lib\ --language C# --working-dir C:\Projects\Experiment --root-namespace Experiment.Lib
Using assembly 'Experiment.Lib'.
Using startup assembly 'Experiment'.
Using application base 'C:\Projects\Experiment\Experiment\bin\Debug\netcoreapp3.0'.
Using working directory 'C:\Projects\Experiment\Experiment'.
Using root namespace 'Experiment.Lib'.
Using project directory 'C:\Projects\Experiment\Experiment.Lib\'.
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 'BranchContext'.
Using context 'BranchContext'.
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 'Experiment'.
No referenced design-time services were found.
Finding IDesignTimeServices implementations in assembly 'Experiment'...
No design-time services were found.
'BranchContext' disposed.
System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.EntityFrameworkCore.Metadata.Internal.MetadataExtensions.AsConcreteMetadataType[TInterface,TConcrete](TInterface interface, String methodName)
   at Microsoft.EntityFrameworkCore.Metadata.Internal.EntityTypeExtensions.AsEntityType(IEntityType entityType, String methodName)
   at Microsoft.EntityFrameworkCore.EntityTypeExtensions.GetDeclaredProperties(IEntityType entityType)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetSortedProperties(IEntityType entityType)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetSortedProperties(TableMapping target)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Add(TableMapping target, DiffContext diffContext)+MoveNext()
   at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.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.

Comment/uncomment the other lines in BranchContext:OnModelCreating() to confirm that this does not occur with some minor changes to the entity configuration logic.

Expected Result

A migration should be generated to create a view on the database that will contain columns that correspond to the properties of Order and Vehicle.

Further technical details

EF Core version: EF Core 3.0 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .NET Core 3 Operating system: Windows 10 64-bit IDE: Visual Studio 2019 16.3

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
AndriySvyrydcommented, Oct 14, 2019

We should throw a better exception until https://github.com/aspnet/EntityFrameworkCore/issues/14497 is implemented.

0reactions
ajcvickerscommented, Oct 18, 2019

Filed https://github.com/aspnet/EntityFrameworkCore/issues/18458 for the migrations part of this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ToView for entity types with owned types throws NRE #18298
Given a domain consisting of two entities X and Y where X owns an instance of Y and both are mapped to a...
Read more >
Owned Entity Types - EF Core
EF Core allows you to model entity types that can only ever appear on navigation properties of other entity types. These are called...
Read more >
Error: the entity type requires a primary key
You need Entity Framework to be able to set the value of ID . This means the property needs to have a setter....
Read more >
What's New in EF Core 5 - Learn Entity Framework Core 7
Use a C# attribute to indicate that an entity has no key. An entity type can now be configured as having no key...
Read more >
Manage entity types | Vertex AI
Learn how to create, list, and delete entity types. Create an entity type. Create an entity type so that you can create its...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found