ToView for entity types with owned types throws NRE
See original GitHub issueGiven 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
- Create a .NET Core 3 console application (
Experiment
). - Install
Microsoft.EntityFrameworkCore.Tools
inExperiment
. - Create a .NET Standard 2.1 C# library (
Experiment.Lib
). - Install
Microsoft.EntityFrameworkCore.SqlServer
inExperiment.Lib
. - Create these classes in
Experiment.Lib
. - Reference
Experiment.Lib
fromExperiment
. - In Visual Studio, open the Package Manager Console and set the default project to
Experiment.Lib
, then runAdd-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:
- Created 4 years ago
- Comments:6 (4 by maintainers)
We should throw a better exception until https://github.com/aspnet/EntityFrameworkCore/issues/14497 is implemented.
Filed https://github.com/aspnet/EntityFrameworkCore/issues/18458 for the migrations part of this.