TPH inheritance with two properties mapped to the same column but different nullability is not allowed #21384
Labels
area-model-building
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
I'd like to model a type hierarchy using TPH inheritance where the same property exists on two derived entities and maps to the same underlying column in a SQL Server DB but has different nullability.
When I create my DbContext and model the entities, making sure I explicitly specify the column name, I end up getting a following exception at runtime:
Steps to reproduce
Let's have entities
Derived1
andDerived2
, both siblings in the type hierarchy with ancestorBase
and mapping to tableBase
with backing column namedProperty
that allows NULL. In my scenario, the propertyProperty
is required inDerived1
but optional inDerived2
. Therefore I don't model the property onBase
entity, but I add the property with the same name and type toDervied1
andDerived2
. The code may look something like this:I came across issue #17820 and its fix 218f92c where the check for different nullability was added. I opened this originally as discussion question 21366 where @AndriySvyryd kindly advised that I open and issue as this is a bug.
Just a thought - I suppose an extra complication would arise in case of nullable value types. In my case above, the property was of type
string
/nvarchar
hence a reference type that allows nulls (we don't use nullable reference types). For example, when the property isdatetime
in DB as that would need to be modeled asDateTime
inDerived1
(required) and DateTime? inDerived2
(optional).Further technical details
EF Core version: 5.0.0-preview.5.20278.2 but should also be reproducible in 3.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 5 preview 5 but also .NET Core 3.1
Operating system: Windows 10 Enterprise build 18363
IDE: Visual Studio 2019 16.6.2
The text was updated successfully, but these errors were encountered: