From 1f12a1e94a01a2bd280fc9be296f11e8e82a9572 Mon Sep 17 00:00:00 2001 From: Andriy Svyryd Date: Thu, 8 Dec 2022 15:15:59 -0800 Subject: [PATCH] Avoid reduntant looping in property metadata Fixes #29642 --- .../CSharpRuntimeModelCodeGenerator.cs | 16 ++++++++--- src/EFCore/Metadata/Internal/Property.cs | 28 +++++++++++++++---- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/EFCore.Design/Scaffolding/Internal/CSharpRuntimeModelCodeGenerator.cs b/src/EFCore.Design/Scaffolding/Internal/CSharpRuntimeModelCodeGenerator.cs index 690e39d60a9..03fa2346c69 100644 --- a/src/EFCore.Design/Scaffolding/Internal/CSharpRuntimeModelCodeGenerator.cs +++ b/src/EFCore.Design/Scaffolding/Internal/CSharpRuntimeModelCodeGenerator.cs @@ -858,6 +858,7 @@ private void Create( var principalProperty = property; for (var i = 0; i < 10000; i++) { + IProperty? nextProperty = null; foreach (var foreignKey in principalProperty.GetContainingForeignKeys()) { for (var propertyIndex = 0; propertyIndex < foreignKey.Properties.Count; propertyIndex++) @@ -865,22 +866,29 @@ private void Create( if (principalProperty == foreignKey.Properties[propertyIndex]) { var newPrincipalProperty = foreignKey.PrincipalKey.Properties[propertyIndex]; - if (property == principalProperty + if (newPrincipalProperty == property || newPrincipalProperty == principalProperty) { break; } - principalProperty = newPrincipalProperty; - - type = (Type?)principalProperty[CoreAnnotationNames.ValueConverterType]; + type = (Type?)newPrincipalProperty[CoreAnnotationNames.ValueConverterType]; if (type != null) { return type; } + + nextProperty = newPrincipalProperty; } } } + + if (nextProperty == null) + { + break; + } + + principalProperty = nextProperty; } return null; diff --git a/src/EFCore/Metadata/Internal/Property.cs b/src/EFCore/Metadata/Internal/Property.cs index 158c1025604..73bbda13483 100644 --- a/src/EFCore/Metadata/Internal/Property.cs +++ b/src/EFCore/Metadata/Internal/Property.cs @@ -670,6 +670,7 @@ public virtual PropertySaveBehavior GetAfterSaveBehavior() var property = this; for (var i = 0; i < 10000; i++) { + Property? nextProperty = null; foreach (var foreignKey in property.GetContainingForeignKeys()) { for (var propertyIndex = 0; propertyIndex < foreignKey.Properties.Count; propertyIndex++) @@ -683,16 +684,23 @@ public virtual PropertySaveBehavior GetAfterSaveBehavior() break; } - property = principalProperty; - - converter = (ValueConverter?)property[CoreAnnotationNames.ValueConverter]; + converter = (ValueConverter?)principalProperty[CoreAnnotationNames.ValueConverter]; if (converter != null) { return converter; } + + nextProperty = principalProperty; } } } + + if (nextProperty == null) + { + break; + } + + property = nextProperty; } return null; @@ -749,6 +757,7 @@ public virtual PropertySaveBehavior GetAfterSaveBehavior() var property = this; for (var i = 0; i < 10000; i++) { + Property? nextProperty = null; foreach (var foreignKey in property.GetContainingForeignKeys()) { for (var propertyIndex = 0; propertyIndex < foreignKey.Properties.Count; propertyIndex++) @@ -762,16 +771,23 @@ public virtual PropertySaveBehavior GetAfterSaveBehavior() break; } - property = principalProperty; - - type = (Type?)property[CoreAnnotationNames.ProviderClrType]; + type = (Type?)principalProperty[CoreAnnotationNames.ProviderClrType]; if (type != null) { return type; } + + nextProperty = principalProperty; } } } + + if (nextProperty == null) + { + break; + } + + property = nextProperty; } return null;