@@ -45,9 +45,7 @@ protected EntityShaperExpression(
45
45
46
46
if ( discriminatorCondition == null )
47
47
{
48
- // Generate condition to discriminator if TPH
49
- discriminatorCondition = GenerateDiscriminatorCondition ( entityType ) ;
50
-
48
+ discriminatorCondition = GenerateDiscriminatorCondition ( entityType , nullable ) ;
51
49
}
52
50
else if ( discriminatorCondition . Parameters . Count != 1
53
51
|| discriminatorCondition . Parameters [ 0 ] . Type != typeof ( ValueBuffer )
@@ -63,11 +61,11 @@ protected EntityShaperExpression(
63
61
DiscriminatorCondition = discriminatorCondition ;
64
62
}
65
63
66
- private LambdaExpression GenerateDiscriminatorCondition ( IEntityType entityType )
64
+ private LambdaExpression GenerateDiscriminatorCondition ( IEntityType entityType , bool nullable )
67
65
{
68
66
var valueBufferParameter = Parameter ( typeof ( ValueBuffer ) ) ;
69
67
Expression body ;
70
- var concreteEntityTypes = entityType . GetConcreteDerivedTypesInclusive ( ) . ToList ( ) ;
68
+ var concreteEntityTypes = entityType . GetConcreteDerivedTypesInclusive ( ) . ToArray ( ) ;
71
69
var discriminatorProperty = entityType . GetDiscriminatorProperty ( ) ;
72
70
if ( discriminatorProperty != null )
73
71
{
@@ -80,8 +78,8 @@ private LambdaExpression GenerateDiscriminatorCondition(IEntityType entityType)
80
78
discriminatorProperty . ClrType , discriminatorProperty . GetIndex ( ) , discriminatorProperty ) )
81
79
} ;
82
80
83
- var switchCases = new SwitchCase [ concreteEntityTypes . Count ] ;
84
- for ( var i = 0 ; i < concreteEntityTypes . Count ; i ++ )
81
+ var switchCases = new SwitchCase [ concreteEntityTypes . Length ] ;
82
+ for ( var i = 0 ; i < concreteEntityTypes . Length ; i ++ )
85
83
{
86
84
var discriminatorValue = Constant ( concreteEntityTypes [ i ] . GetDiscriminatorValue ( ) , discriminatorProperty . ClrType ) ;
87
85
switchCases [ i ] = SwitchCase ( Constant ( concreteEntityTypes [ i ] , typeof ( IEntityType ) ) , discriminatorValue ) ;
@@ -97,7 +95,20 @@ private LambdaExpression GenerateDiscriminatorCondition(IEntityType entityType)
97
95
}
98
96
else
99
97
{
100
- body = Constant ( concreteEntityTypes . Count == 1 ? concreteEntityTypes [ 0 ] : entityType , typeof ( IEntityType ) ) ;
98
+ body = Constant ( concreteEntityTypes . Length == 1 ? concreteEntityTypes [ 0 ] : entityType , typeof ( IEntityType ) ) ;
99
+ }
100
+
101
+ if ( entityType . FindPrimaryKey ( ) == null
102
+ && nullable )
103
+ {
104
+ body = Condition (
105
+ entityType . GetProperties ( )
106
+ . Select ( p => NotEqual (
107
+ valueBufferParameter . CreateValueBufferReadValueExpression ( typeof ( object ) , p . GetIndex ( ) , p ) ,
108
+ Constant ( null ) ) )
109
+ . Aggregate ( ( a , b ) => OrElse ( a , b ) ) ,
110
+ body ,
111
+ Default ( typeof ( IEntityType ) ) ) ;
101
112
}
102
113
103
114
return Lambda ( body , valueBufferParameter ) ;
@@ -128,7 +139,8 @@ public virtual EntityShaperExpression WithEntityType([NotNull] IEntityType entit
128
139
129
140
public virtual EntityShaperExpression MarkAsNullable ( )
130
141
=> ! IsNullable
131
- ? new EntityShaperExpression ( EntityType , ValueBufferExpression , true , DiscriminatorCondition )
142
+ // Marking nullable requires recomputation of Discriminator condition
143
+ ? new EntityShaperExpression ( EntityType , ValueBufferExpression , true )
132
144
: this ;
133
145
134
146
public virtual EntityShaperExpression Update ( [ NotNull ] Expression valueBufferExpression )
0 commit comments