@@ -76,11 +76,10 @@ private void ProcessEntityType(IConventionEntityType entityType, IConventionCont
76
76
var computedIdProperty = entityType . FindDeclaredProperty ( DefaultIdPropertyName ) ;
77
77
78
78
var primaryKey = entityType . FindPrimaryKey ( ) ;
79
- if ( entityType . BaseType != null // Requires: IEntityTypeBaseTypeChangedConvention
80
- || ! entityType . IsDocumentRoot ( ) // Requires: IEntityTypeAnnotationChangedConvention (ContainerName)
81
- || entityType . GetForeignKeys ( )
82
- . Any ( fk => fk . IsOwnership ) // Requires: IForeignKeyOwnershipChangedConvention, IForeignKeyRemovedConvention
83
- || primaryKey == null ) // Requires: IKeyAddedConvention, IKeyRemovedConvention
79
+ if ( entityType . BaseType != null
80
+ || ! entityType . IsDocumentRoot ( )
81
+ || entityType . GetForeignKeys ( ) . Any ( fk => fk . IsOwnership )
82
+ || primaryKey == null )
84
83
{
85
84
// If the entity type is not a keyed, root document in the container, then it doesn't have an `id` mapping, so
86
85
// undo anything that was done by previous execution of this convention.
@@ -93,7 +92,8 @@ private void ProcessEntityType(IConventionEntityType entityType, IConventionCont
93
92
if ( computedIdProperty is not null
94
93
&& computedIdProperty != jsonIdProperty )
95
94
{
96
- entityType . Builder . RemoveUnusedImplicitProperties ( [ computedIdProperty ] ) ; }
95
+ entityType . Builder . RemoveUnusedImplicitProperties ( [ computedIdProperty ] ) ;
96
+ }
97
97
98
98
return ;
99
99
}
@@ -102,17 +102,9 @@ private void ProcessEntityType(IConventionEntityType entityType, IConventionCont
102
102
// key is represented by a single string property, and the discriminator is not being included in the JSON `id`.
103
103
// If these conditions are not met, or if the user has opted-in, then we will create a computed property that transforms
104
104
// the appropriate values into a single string for the JSON `id` property.
105
-
106
- // The line below requires: IModelAnnotationChangedConvention, IPropertyAnnotationChangedConvention
107
105
var alwaysCreateId = entityType . GetHasShadowId ( ) ;
108
106
if ( alwaysCreateId != true )
109
107
{
110
- // The line below requires:
111
- // - IModelAnnotationChangedConvention, IPropertyAnnotationChangedConvention
112
- // - IKeyAddedConvention, IKeyRemovedConvention
113
- // - IPropertyAddedConvention, IPropertyRemovedConvention
114
- // - IDiscriminatorPropertySetConvention
115
- // - IEntityTypeBaseTypeChangedConvention
116
108
var idDefinition = DefinitionFactory . Create ( ( IEntityType ) entityType ) ! ;
117
109
if ( idDefinition is { IncludesDiscriminator : false , Properties . Count : 1 } )
118
110
{
@@ -125,15 +117,16 @@ private void ProcessEntityType(IConventionEntityType entityType, IConventionCont
125
117
?? mapping ? . ClrType
126
118
?? keyProperty ! . ClrType ;
127
119
128
- if ( clrType == typeof ( string ) )
120
+ if ( clrType == typeof ( string )
121
+ && keyProperty . Builder . CanSetJsonProperty ( IdPropertyJsonName ) )
129
122
{
130
123
// We are at the point where we are going to map the `id` directly to the PK.
131
124
// However, if a previous run of this convention create the computed property, then we need to remove that
132
125
// mapping since it is now not needed.
133
- if ( computedIdProperty != null )
126
+ if ( computedIdProperty != null
127
+ && entityType . Builder . HasNoProperty ( computedIdProperty ) == null )
134
128
{
135
129
computedIdProperty . Builder . ToJsonProperty ( null ) ;
136
- entityType . Builder . HasNoProperty ( computedIdProperty ) ;
137
130
}
138
131
139
132
// If there was previously a different property mapped to `id`, but not one of our computed properties,
@@ -146,10 +139,7 @@ private void ProcessEntityType(IConventionEntityType entityType, IConventionCont
146
139
}
147
140
148
141
// Finally, actually map the primary key directly to the JSON `id`.
149
- if ( keyProperty . GetJsonPropertyName ( ) != IdPropertyJsonName )
150
- {
151
- keyProperty . Builder . ToJsonProperty ( IdPropertyJsonName ) ;
152
- }
142
+ keyProperty . Builder . ToJsonProperty ( IdPropertyJsonName ) ;
153
143
154
144
return ;
155
145
}
@@ -183,13 +173,10 @@ private void ProcessEntityType(IConventionEntityType entityType, IConventionCont
183
173
return ;
184
174
}
185
175
186
- if ( computedIdPropertyBuilder . Metadata . GetJsonPropertyName ( ) != IdPropertyJsonName )
187
- {
188
- computedIdPropertyBuilder = computedIdPropertyBuilder . ToJsonProperty ( IdPropertyJsonName )
189
- ?? computedIdPropertyBuilder ;
190
- }
191
-
192
176
// Don't chain, because each of these could return null if the property has been explicitly configured with some other value.
177
+ computedIdPropertyBuilder = computedIdPropertyBuilder . ToJsonProperty ( IdPropertyJsonName )
178
+ ?? computedIdPropertyBuilder ;
179
+
193
180
computedIdPropertyBuilder = computedIdPropertyBuilder . IsRequired ( true )
194
181
?? computedIdPropertyBuilder ;
195
182
0 commit comments