-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Milestone
Description
Bug
Retrieving entities of the base type where one child has a required navigation property throws a SqlNullValuException
. Because not all entities have a required navigation property. Property.IsNullable
is expected to be true when reading properties navigation.
Source code
Stack traces
System.Data.SqlTypes.SqlNullValueException: Data is Null. This method or property cannot be called on Null values.
at Microsoft.Data.SqlClient.SqlBuffer.ThrowIfNull()
at Microsoft.Data.SqlClient.SqlBuffer.get_Guid()
at Microsoft.Data.SqlClient.SqlDataReader.GetGuid(Int32 i)
at lambda_method62(Closure , QueryContext , DbDataReader , ResultContext , SingleQueryResultCoordinator )
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
System.Data.SqlTypes.SqlNullValueException: Data is Null. This method or property cannot be called on Null values.
at Microsoft.Data.SqlClient.SqlBuffer.ThrowIfNull()
at Microsoft.Data.SqlClient.SqlBuffer.get_Guid()
at Microsoft.Data.SqlClient.SqlDataReader.GetGuid(Int32 i)
at lambda_method62(Closure , QueryContext , DbDataReader , ResultContext , SingleQueryResultCoordinator )
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
Verbose output
Need to run test
C:\Program Files\dotnet\dotnet.exe exec --runtimeConfig E:\Work\projects\EF.HierarchyWithOwnsOne\Tests\bin\Debug\net6.0\Tests.runtimeconfig.json --depsfile E:\Work\projects\EF.HierarchyWithOwnsOne\Tests\bin\Debug\net6.0\Tests.deps.json "C:\Program Files\JetBrains\Rider\r2r\2023.1.2R\6B49094255C5915777A714B5EEA9433\TestRunner\netcoreapp3.0\ReSharperTestRunner.dll" --parentProcessId 65984 -p 62324 -r 6c65adf1-fb86-41e5-8d68-82986246c9e9
...
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT [e].[Id], [e].[Discriminator], [e].[Name], [c].[Id], [c].[Child1EntityId], [c].[Name]
FROM [Entities] AS [e]
LEFT JOIN [Child1EntityData] AS [c] ON [e].[Id] = [c].[Child1EntityId]
fail: Microsoft.EntityFrameworkCore.Query[10100]
An exception occurred while iterating over the results of a query for context type 'DataAccess.AppDbContext'.
System.Data.SqlTypes.SqlNullValueException: Data is Null. This method or property cannot be called on Null values.
at Microsoft.Data.SqlClient.SqlBuffer.ThrowIfNull()
at Microsoft.Data.SqlClient.SqlBuffer.get_Guid()
at Microsoft.Data.SqlClient.SqlDataReader.GetGuid(Int32 i)
at lambda_method62(Closure , QueryContext , DbDataReader , ResultContext , SingleQueryResultCoordinator )
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
System.Data.SqlTypes.SqlNullValueException: Data is Null. This method or property cannot be called on Null values.
at Microsoft.Data.SqlClient.SqlBuffer.ThrowIfNull()
at Microsoft.Data.SqlClient.SqlBuffer.get_Guid()
at Microsoft.Data.SqlClient.SqlDataReader.GetGuid(Int32 i)
at lambda_method62(Closure , QueryContext , DbDataReader , ResultContext , SingleQueryResultCoordinator )
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()
Temporary solution
Have options:
- Set navigation property as nullable
- Add
<entityBuilder>.Metadata.IsRequiredDependent = false;
to navigation configuration
Provider and version information
EF Core version: 7.0.5
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0
Nemo-Illusionist, DanilovSoft and Symasoiti97