diff --git a/src/EFCore.SqlServer/Infrastructure/Internal/SqlServerModelValidator.cs b/src/EFCore.SqlServer/Infrastructure/Internal/SqlServerModelValidator.cs index 9dc3f76bf27..5509781a84c 100644 --- a/src/EFCore.SqlServer/Infrastructure/Internal/SqlServerModelValidator.cs +++ b/src/EFCore.SqlServer/Infrastructure/Internal/SqlServerModelValidator.cs @@ -101,20 +101,34 @@ protected virtual void ValidateVectorColumns( IModel model, IDiagnosticsLogger logger) { - foreach (IConventionProperty property in model.GetEntityTypes() - .SelectMany(t => t.GetDeclaredProperties()) - .Where(p => p.ClrType.UnwrapNullableType() == typeof(SqlVector))) + foreach (var entityType in model.GetEntityTypes()) { - if (property.GetTypeMapping() is not SqlServerVectorTypeMapping { Size: not null } vectorTypeMapping) + ValidateVectorColumns(entityType); + } + + void ValidateVectorColumns(ITypeBase typeBase) + { + foreach (var property in typeBase.GetDeclaredProperties()) { - throw new InvalidOperationException( - SqlServerStrings.VectorDimensionsMissing(property.DeclaringType.DisplayName(), property.Name)); + if (property.FindTypeMapping() is SqlServerVectorTypeMapping vectorTypeMapping) + { + if (property.DeclaringType.IsMappedToJson()) + { + throw new InvalidOperationException( + SqlServerStrings.VectorPropertiesNotSupportedInJson(property.DeclaringType.DisplayName(), property.Name)); + } + + if (vectorTypeMapping.Size is null) + { + throw new InvalidOperationException( + SqlServerStrings.VectorDimensionsMissing(property.DeclaringType.DisplayName(), property.Name)); + } + } } - if (property.DeclaringType.IsMappedToJson()) + foreach (var complexProperty in typeBase.GetDeclaredComplexProperties()) { - throw new InvalidOperationException( - SqlServerStrings.VectorPropertiesNotSupportedInJson(property.DeclaringType.DisplayName(), property.Name)); + ValidateVectorColumns(complexProperty.ComplexType); } } } diff --git a/test/EFCore.SqlServer.Tests/Infrastructure/SqlServerModelValidatorTest.cs b/test/EFCore.SqlServer.Tests/Infrastructure/SqlServerModelValidatorTest.cs index 64b94466c56..4272fd8d317 100644 --- a/test/EFCore.SqlServer.Tests/Infrastructure/SqlServerModelValidatorTest.cs +++ b/test/EFCore.SqlServer.Tests/Infrastructure/SqlServerModelValidatorTest.cs @@ -1105,7 +1105,7 @@ public virtual void Throws_for_vector_property_without_dimensions() } [ConditionalFact] - public virtual void Throws_for_vector_property_inside_JSON() + public virtual void Throws_for_vector_property_inside_JSON_owned_entity() { var modelBuilder = CreateConventionModelBuilder(); @@ -1121,6 +1121,27 @@ public virtual void Throws_for_vector_property_inside_JSON() modelBuilder); } + [ConditionalFact] + public virtual void Throws_for_vector_property_inside_JSON_complex_type() + { + var modelBuilder = CreateConventionModelBuilder(); + + modelBuilder.Entity() + .ComplexProperty( + v => v.VectorContainer, + n => + { + n.ToJson(); + n.Property(v => v.Vector).HasMaxLength(3); + }); + + VerifyError( + SqlServerStrings.VectorPropertiesNotSupportedInJson( + "VectorInsideJsonEntity.VectorContainer#VectorContainer", + nameof(VectorContainer.Vector)), + modelBuilder); + } + public class VectorWithoutDimensionsEntity { public int Id { get; set; }