Skip to content

Commit 68be673

Browse files
committed
Make GetFlattened*() methods return property within complex collections by default
1 parent 08bb37e commit 68be673

File tree

20 files changed

+163
-178
lines changed

20 files changed

+163
-178
lines changed

src/EFCore.Relational/Query/RelationalQueryableMethodTranslatingExpressionVisitor.CreateSelect.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,11 @@ [new TableExpression(alias, table)],
144144
else
145145
{
146146
var tables = entityTypes.Select(e => e.GetViewOrTableMappings().Single().Table).ToArray();
147-
var properties = entityType.GetFlattenedPropertiesInHierarchy().ToArray();
147+
var properties = entityType.GetFlattenedPropertiesInHierarchy(withinComplexCollections: false).ToArray();
148148
var propertyNamesMap = new Dictionary<IProperty, string>();
149149
for (var i = 0; i < entityTypes.Length; i++)
150150
{
151-
foreach (var property in entityTypes[i].GetFlattenedProperties())
151+
foreach (var property in entityTypes[i].GetFlattenedProperties(withinComplexCollections: false))
152152
{
153153
if (!propertyNamesMap.ContainsKey(property))
154154
{

src/EFCore.Relational/Update/ModificationCommand.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ private List<IColumnModification> GenerateColumnModifications()
290290

291291
if (_entries.Any(e => e.EntityType is { } entityType
292292
&& (entityType.IsMappedToJson()
293-
|| entityType.GetFlattenedComplexProperties().Any(cp => cp.ComplexType.IsMappedToJson())
293+
|| entityType.GetFlattenedComplexProperties(withinComplexCollections: false).Any(cp => cp.ComplexType.IsMappedToJson())
294294
|| entityType.GetNavigations().Any(e => e.IsCollection && e.TargetEntityType.IsMappedToJson()))))
295295
{
296296
HandleJson(columnModifications);
@@ -617,7 +617,9 @@ void HandleSharedColumns(
617617
result.Insert(0, pathEntry);
618618
}
619619

620-
var modifiedMembers = entry.EntityType.GetFlattenedProperties().Where(entry.IsModified).ToList();
620+
var modifiedMembers = entry.EntityType.GetFlattenedProperties(withinComplexCollections: false)
621+
.Where(entry.IsModified)
622+
.ToList();
621623
if (modifiedMembers.Count == 1)
622624
{
623625
result.Add(
@@ -732,7 +734,7 @@ void HandleJson(List<IColumnModification> columnModifications)
732734
}
733735
}
734736

735-
foreach (var complexProperty in entry.EntityType.GetFlattenedComplexProperties())
737+
foreach (var complexProperty in entry.EntityType.GetFlattenedComplexProperties(withinComplexCollections: false))
736738
{
737739
var complexType = complexProperty.ComplexType;
738740
if (!complexType.IsMappedToJson()

src/EFCore.SqlServer/Infrastructure/Internal/SqlServerModelValidator.cs

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -101,34 +101,23 @@ protected virtual void ValidateVectorColumns(
101101
IModel model,
102102
IDiagnosticsLogger<DbLoggerCategory.Model.Validation> logger)
103103
{
104-
foreach (var entityType in model.GetEntityTypes())
105-
{
106-
ValidateVectorColumns(entityType);
107-
}
108-
109-
void ValidateVectorColumns(ITypeBase typeBase)
104+
foreach (var property in model.GetEntityTypes().SelectMany(t => t.GetFlattenedDeclaredProperties()))
110105
{
111-
foreach (var property in typeBase.GetDeclaredProperties())
106+
if (property.FindTypeMapping() is not SqlServerVectorTypeMapping vectorTypeMapping)
112107
{
113-
if (property.GetTypeMapping() is SqlServerVectorTypeMapping vectorTypeMapping)
114-
{
115-
if (property.DeclaringType.IsMappedToJson())
116-
{
117-
throw new InvalidOperationException(
118-
SqlServerStrings.VectorPropertiesNotSupportedInJson(property.DeclaringType.DisplayName(), property.Name));
119-
}
108+
continue;
109+
}
120110

121-
if (vectorTypeMapping.Size is null)
122-
{
123-
throw new InvalidOperationException(
124-
SqlServerStrings.VectorDimensionsMissing(property.DeclaringType.DisplayName(), property.Name));
125-
}
126-
}
111+
if (property.DeclaringType.IsMappedToJson())
112+
{
113+
throw new InvalidOperationException(
114+
SqlServerStrings.VectorPropertiesNotSupportedInJson(property.DeclaringType.DisplayName(), property.Name));
127115
}
128116

129-
foreach (var complexProperty in typeBase.GetDeclaredComplexProperties())
117+
if (vectorTypeMapping.Size is null)
130118
{
131-
ValidateVectorColumns(complexProperty.ComplexType);
119+
throw new InvalidOperationException(
120+
SqlServerStrings.VectorDimensionsMissing(property.DeclaringType.DisplayName(), property.Name));
132121
}
133122
}
134123
}
@@ -191,20 +180,18 @@ protected override void ValidateTypeMappings(
191180
var strategy = property.GetValueGenerationStrategy();
192181
var propertyType = property.ClrType;
193182

194-
if (strategy == SqlServerValueGenerationStrategy.IdentityColumn
195-
&& !SqlServerPropertyExtensions.IsCompatibleWithValueGeneration(property))
196-
{
197-
throw new InvalidOperationException(
198-
SqlServerStrings.IdentityBadType(
199-
property.Name, property.DeclaringType.DisplayName(), propertyType.ShortDisplayName()));
200-
}
201-
202-
if (strategy is SqlServerValueGenerationStrategy.SequenceHiLo or SqlServerValueGenerationStrategy.Sequence
203-
&& !SqlServerPropertyExtensions.IsCompatibleWithValueGeneration(property))
183+
switch (property.GetValueGenerationStrategy())
204184
{
205-
throw new InvalidOperationException(
206-
SqlServerStrings.SequenceBadType(
207-
property.Name, property.DeclaringType.DisplayName(), propertyType.ShortDisplayName()));
185+
case SqlServerValueGenerationStrategy.IdentityColumn
186+
when !SqlServerPropertyExtensions.IsCompatibleWithValueGeneration(property):
187+
throw new InvalidOperationException(
188+
SqlServerStrings.IdentityBadType(
189+
property.Name, property.DeclaringType.DisplayName(), propertyType.ShortDisplayName()));
190+
case SqlServerValueGenerationStrategy.SequenceHiLo or SqlServerValueGenerationStrategy.Sequence
191+
when !SqlServerPropertyExtensions.IsCompatibleWithValueGeneration(property):
192+
throw new InvalidOperationException(
193+
SqlServerStrings.SequenceBadType(
194+
property.Name, property.DeclaringType.DisplayName(), propertyType.ShortDisplayName()));
208195
}
209196
}
210197
}

src/EFCore/ChangeTracking/ComplexElementEntry.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ public ComplexElementEntry(InternalComplexEntry internalEntry)
5050
/// </remarks>
5151
public virtual bool IsModified
5252
{
53-
get => Metadata.ComplexType.GetFlattenedProperties().Any(InternalEntry.IsModified)
54-
|| Metadata.ComplexType.GetFlattenedComplexProperties().Any(c => c.IsCollection && InternalEntry.IsModified(c));
53+
get => Metadata.ComplexType.GetFlattenedProperties(withinComplexCollections: false).Any(InternalEntry.IsModified)
54+
|| Metadata.ComplexType.GetFlattenedComplexProperties(withinComplexCollections: false)
55+
.Any(c => c.IsCollection && InternalEntry.IsModified(c));
5556
set
5657
{
57-
foreach (var property in Metadata.ComplexType.GetFlattenedProperties())
58+
foreach (var property in Metadata.ComplexType.GetFlattenedProperties(withinComplexCollections: false))
5859
{
5960
InternalEntry.SetPropertyModified(property, isModified: value);
6061
}

src/EFCore/ChangeTracking/ComplexPropertyEntry.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,17 @@ public ComplexPropertyEntry(IInternalEntry internalEntry, IComplexProperty compl
5555
/// </remarks>
5656
public override bool IsModified
5757
{
58-
get => Metadata.ComplexType.GetFlattenedProperties().Any(InternalEntry.IsModified)
59-
|| Metadata.ComplexType.GetFlattenedComplexProperties().Any(c => c.IsCollection && InternalEntry.IsModified(c));
58+
get => Metadata.ComplexType.GetFlattenedProperties(withinComplexCollections: false).Any(InternalEntry.IsModified)
59+
|| Metadata.ComplexType.GetFlattenedComplexProperties(withinComplexCollections: false)
60+
.Any(c => c.IsCollection && InternalEntry.IsModified(c));
6061
set
6162
{
62-
foreach (var property in Metadata.ComplexType.GetFlattenedProperties())
63+
foreach (var property in Metadata.ComplexType.GetFlattenedProperties(withinComplexCollections: false))
6364
{
6465
InternalEntry.SetPropertyModified(property, isModified: value);
6566
}
6667

67-
foreach (var complexProperty in Metadata.ComplexType.GetFlattenedComplexProperties())
68+
foreach (var complexProperty in Metadata.ComplexType.GetFlattenedComplexProperties(withinComplexCollections: false))
6869
{
6970
if (!complexProperty.IsCollection)
7071
{

src/EFCore/ChangeTracking/Internal/ArrayPropertyValues.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ private void SetValuesFromDictionary<TProperty>(IRuntimeTypeBase structuralType,
384384

385385
var complexEntry = new InternalComplexEntry((IRuntimeComplexType)complexProperty.ComplexType, InternalEntry, i);
386386
var complexType = complexEntry.StructuralType;
387-
var values = new object?[complexType.GetFlattenedProperties().Count()];
387+
var values = new object?[complexType.GetFlattenedProperties(withinComplexCollections: false).Count()];
388388
var complexPropertyValues = new ArrayPropertyValues(complexEntry, values, null);
389389
complexPropertyValues.SetValues(itemDict);
390390

@@ -489,7 +489,7 @@ internal void SetComplexCollectionValue(IComplexProperty complexProperty, List<A
489489
ArrayPropertyValues CreateComplexPropertyValues(object complexObject, InternalComplexEntry entry)
490490
{
491491
var complexType = entry.StructuralType;
492-
var properties = complexType.GetFlattenedProperties().AsList();
492+
var properties = complexType.GetFlattenedProperties(withinComplexCollections: false).AsList();
493493
var values = new object?[properties.Count];
494494

495495
for (var i = 0; i < properties.Count; i++)

src/EFCore/ChangeTracking/Internal/ChangeDetector.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ private bool LocalDetectChanges(InternalEntityEntry entry)
265265
private bool LocalDetectChanges(InternalEntryBase entry)
266266
{
267267
var changesFound = false;
268-
foreach (var property in entry.StructuralType.GetFlattenedProperties())
268+
foreach (var property in entry.StructuralType.GetFlattenedProperties(withinComplexCollections: false))
269269
{
270270
if (property.GetOriginalValueIndex() >= 0
271271
&& !entry.IsModified(property)
@@ -283,7 +283,7 @@ private bool LocalDetectChanges(InternalEntryBase entry)
283283
}
284284
}
285285

286-
foreach (var complexProperty in entry.StructuralType.GetFlattenedComplexProperties())
286+
foreach (var complexProperty in entry.StructuralType.GetFlattenedComplexProperties(withinComplexCollections: false))
287287
{
288288
if (complexProperty.IsCollection)
289289
{

src/EFCore/ChangeTracking/Internal/InternalEntityEntry.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public InternalEntityEntry(
6666
{
6767
StateManager = stateManager;
6868
var valuesArray = new object?[EntityType.PropertyCount];
69-
foreach (var property in entityType.GetFlattenedProperties())
69+
foreach (var property in entityType.GetFlattenedProperties(withinComplexCollections: false))
7070
{
7171
var index = property.GetIndex();
7272
if (index < 0)
@@ -762,8 +762,8 @@ public override void HandleConceptualNulls(bool sensitiveLoggingEnabled, bool fo
762762
}
763763
else
764764
{
765-
var property = EntityType.GetFlattenedProperties().FirstOrDefault(p => (EntityState != EntityState.Modified
766-
|| IsModified(p))
765+
var property = EntityType.GetFlattenedProperties(withinComplexCollections: false)
766+
.FirstOrDefault(p => (EntityState != EntityState.Modified || IsModified(p))
767767
&& PropertyStateData.IsPropertyFlagged(p.GetIndex(), PropertyFlag.Null));
768768

769769
if (property != null)
@@ -907,8 +907,8 @@ private static IEnumerable<IPropertyBase> GetNotificationProperties(
907907
{
908908
if (string.IsNullOrEmpty(propertyName))
909909
{
910-
foreach (var property in entityType.GetFlattenedProperties()
911-
.Where(p => p.GetAfterSaveBehavior() == PropertySaveBehavior.Save))
910+
foreach (var property in entityType.GetFlattenedProperties(withinComplexCollections: false)
911+
.Where(p => p.GetAfterSaveBehavior() == PropertySaveBehavior.Save))
912912
{
913913
yield return property;
914914
}

0 commit comments

Comments
 (0)