Skip to content

Commit 3142eb0

Browse files
[release/11.0] Validate unsupported SQL Server JSON index options (#39090)
Fixes #39065 Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
1 parent d8e07a4 commit 3142eb0

4 files changed

Lines changed: 123 additions & 0 deletions

File tree

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,13 @@ protected override void ValidateIndex(
229229
ValidateIndexIncludeProperties(index);
230230
ValidateFullTextIndex(index);
231231
ValidateVectorIndex(index);
232+
233+
#pragma warning disable EF1001 // Internal EF Core API usage.
234+
if (index.IsJsonIndex())
235+
#pragma warning restore EF1001 // Internal EF Core API usage.
236+
{
237+
ValidateUnsupportedJsonIndexOptions(index);
238+
}
232239
}
233240

234241
/// <summary>
@@ -425,6 +432,29 @@ protected virtual void ValidateVectorIndex(IIndex index)
425432
}
426433
}
427434

435+
private static void ValidateUnsupportedJsonIndexOptions(IIndex index)
436+
{
437+
var option = index switch
438+
{
439+
{ IsUnique: true } => nameof(index.IsUnique),
440+
{ IsDescending: not null } => nameof(index.IsDescending),
441+
_ when index.GetFilter() is not null => "Filter",
442+
_ when index.IsClustered() is true => "IsClustered",
443+
_ when index.GetIncludeProperties()?.Count > 0 => "IncludeProperties",
444+
_ when index.IsCreatedOnline() is true => "IsCreatedOnline",
445+
_ when index.GetSortInTempDb() is true => "SortInTempDb",
446+
_ when index.GetDataCompression() is not null => "DataCompression",
447+
_ => null
448+
};
449+
450+
if (option is not null)
451+
{
452+
throw new InvalidOperationException(
453+
SqlServerStrings.JsonIndexUnsupportedOption(
454+
index.DisplayName(), index.DeclaringEntityType.DisplayName(), option));
455+
}
456+
}
457+
428458
private static void ValidateUnsupportedIndexOptions(IIndex index, Func<string, string> errorFactory)
429459
{
430460
var option = index switch

‎src/EFCore.SqlServer/Properties/SqlServerStrings.Designer.cs‎

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/EFCore.SqlServer/Properties/SqlServerStrings.resx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@
231231
<data name="InvalidTableToIncludeInScaffolding" xml:space="preserve">
232232
<value>The specified table '{table}' is not in a valid format. Specify tables using the format '[schema].[table]'.</value>
233233
</data>
234+
<data name="JsonIndexUnsupportedOption" xml:space="preserve">
235+
<value>JSON index '{index}' on entity type '{entityType}' was configured with the '{option}' option, which is not supported on JSON indexes.</value>
236+
</data>
234237
<data name="JsonValuePathExpressionsNotSupported" xml:space="preserve">
235238
<value>A non-constant array index or property name was used when navigating inside a JSON document, but EF Core's SQL Server compatibility level is set to {compatibilityLevel}; this is only supported with compatibility level 140 (SQL Server 2017) or higher.</value>
236239
</data>

‎test/EFCore.SqlServer.Tests/Infrastructure/SqlServerModelValidatorTest.cs‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,88 @@ protected class EntityWithIncludedComplexAddress
711711
public required string Street { get; set; }
712712
}
713713

714+
[Theory]
715+
[InlineData("IsUnique")]
716+
[InlineData("IsDescending")]
717+
[InlineData("Filter")]
718+
[InlineData("IsClustered")]
719+
[InlineData("IncludeProperties")]
720+
[InlineData("IsCreatedOnline")]
721+
[InlineData("SortInTempDb")]
722+
[InlineData("DataCompression")]
723+
public void Json_index_with_unsupported_option_throws(string option)
724+
{
725+
var modelBuilder = CreateConventionModelBuilder();
726+
modelBuilder.Entity<EntityWithIncludedComplexJson>(b =>
727+
{
728+
b.ComplexProperty(e => e.Address, cb => cb.ToJson());
729+
var indexBuilder = b.HasIndex("Address.City");
730+
731+
switch (option)
732+
{
733+
case "IsUnique":
734+
indexBuilder.IsUnique();
735+
break;
736+
case "IsDescending":
737+
indexBuilder.IsDescending();
738+
break;
739+
case "Filter":
740+
indexBuilder.HasFilter("[Id] > 0");
741+
break;
742+
case "IsClustered":
743+
indexBuilder.IsClustered();
744+
break;
745+
case "IncludeProperties":
746+
indexBuilder.IncludeProperties("Id");
747+
break;
748+
case "IsCreatedOnline":
749+
indexBuilder.IsCreatedOnline();
750+
break;
751+
case "SortInTempDb":
752+
indexBuilder.SortInTempDb();
753+
break;
754+
case "DataCompression":
755+
indexBuilder.Metadata.SetDataCompression(DataCompressionType.Page);
756+
break;
757+
default:
758+
throw new InvalidOperationException();
759+
}
760+
});
761+
762+
VerifyError(
763+
SqlServerStrings.JsonIndexUnsupportedOption(
764+
"{'City'}", nameof(EntityWithIncludedComplexJson), option),
765+
modelBuilder);
766+
}
767+
768+
[Theory]
769+
[InlineData("FillFactor")]
770+
[InlineData("NonClustered")]
771+
[InlineData("NoIncludeProperties")]
772+
[InlineData("CreatedOffline")]
773+
[InlineData("SortInTempDbDisabled")]
774+
public void Json_index_with_supported_option_passes(string option)
775+
{
776+
var modelBuilder = CreateConventionModelBuilder();
777+
modelBuilder.Entity<EntityWithIncludedComplexJson>(b =>
778+
{
779+
b.ComplexProperty(e => e.Address, cb => cb.ToJson());
780+
var indexBuilder = b.HasIndex("Address.City");
781+
782+
_ = option switch
783+
{
784+
"FillFactor" => indexBuilder.HasFillFactor(80),
785+
"NonClustered" => indexBuilder.IsClustered(false),
786+
"NoIncludeProperties" => indexBuilder.IncludeProperties(),
787+
"CreatedOffline" => indexBuilder.IsCreatedOnline(false),
788+
"SortInTempDbDisabled" => indexBuilder.SortInTempDb(false),
789+
_ => throw new InvalidOperationException()
790+
};
791+
});
792+
793+
Validate(modelBuilder);
794+
}
795+
714796
[Fact]
715797
public virtual void Detects_incompatible_memory_optimized_shared_table()
716798
{

0 commit comments

Comments
 (0)