Skip to content

Commit ecdd038

Browse files
Normalize provider annotations to handle JSON elements in complex index processing. Fixes invalid method annotation result in case of composite indexes.
1 parent e57c161 commit ecdd038

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>2.0.0</Version>
3+
<Version>2.0.2</Version>
44
<Authors>CaffeinatedCoder</Authors>
55
<PackageLicenseExpression>MIT</PackageLicenseExpression>
66
<GenerateDocumentationFile>true</GenerateDocumentationFile>

EFCore.ComplexIndexes/CustomMigrationsModelDiffer.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Text.Json;
12
using Microsoft.EntityFrameworkCore;
23
using Microsoft.EntityFrameworkCore.Metadata;
34
using Microsoft.EntityFrameworkCore.Migrations;
@@ -180,6 +181,8 @@ HashSet<IndexDescriptor> results
180181

181182
var indexName = def.IndexName ?? $"IX_{tableName}_{string.Join("_", columnNames)}";
182183

184+
var normalized = NormalizeProviderAnnotations(def.ProviderAnnotations);
185+
183186
results.Add(
184187
new IndexDescriptor(
185188
tableName,
@@ -188,12 +191,44 @@ HashSet<IndexDescriptor> results
188191
indexName,
189192
def.IsUnique,
190193
def.Filter,
191-
def.ProviderAnnotations ?? []
194+
normalized
192195
)
193196
);
194197
}
195198
}
196199

200+
private static Dictionary<string, object?> NormalizeProviderAnnotations(Dictionary<string, object?>? annotations)
201+
{
202+
if (annotations is null) return [];
203+
204+
var result = new Dictionary<string, object?>(annotations.Count);
205+
206+
foreach (var (key, value) in annotations)
207+
{
208+
result[key] = value is JsonElement je
209+
? NormalizeJsonElement(je)
210+
: value;
211+
}
212+
213+
return result;
214+
}
215+
216+
private static object? NormalizeJsonElement(JsonElement je)
217+
{
218+
return je.ValueKind switch
219+
{
220+
JsonValueKind.String => je.GetString(),
221+
JsonValueKind.True => true,
222+
JsonValueKind.False => false,
223+
JsonValueKind.Number => je.TryGetInt64(out var l) ? l : je.GetDouble(),
224+
JsonValueKind.Null => null,
225+
JsonValueKind.Array => je.EnumerateArray()
226+
.Select(e => e.ValueKind == JsonValueKind.String ? e.GetString() : e.ToString())
227+
.ToArray(),
228+
_ => je.ToString()
229+
};
230+
}
231+
197232
private static string? ResolveColumnName(IEntityType entityType, string dotPath)
198233
{
199234
var parts = dotPath.Split('.');

0 commit comments

Comments
 (0)