You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
SetTsVectorProperties throws InvalidCastException when passed a non-array IReadOnlyList<string> #3892
NpgsqlPropertyExtensions.SetTsVectorProperties accepts IReadOnlyList<string>?, but the corresponding getter, GetTsVectorProperties, reads the annotation back with a hard cast to string[]?:
public static IReadOnlyList<string>? GetTsVectorProperties(this IReadOnlyProperty property)
=> (string[]?)property[NpgsqlAnnotationNames.TsVectorProperties];
public static void SetTsVectorProperties(
this IMutableProperty property,
IReadOnlyList<string>? properties)
{
Check.NullButNotEmpty(properties, nameof(properties));
property.SetOrRemoveAnnotation(NpgsqlAnnotationNames.TsVectorProperties, properties);
}
The exception is triggered whenever the value passed to SetTsVectorProperties isn't actually a string[]. This happens with direct metadata usage, for example:
using Microsoft.EntityFrameworkCore;
using NpgsqlTypes;
using var context = new BlogContext(
new DbContextOptionsBuilder<BlogContext>().UseNpgsql("Host=localhost;Database=repro").Options);
context.Database.GenerateCreateScript();
public class BlogContext(DbContextOptions<BlogContext> options) : DbContext(options)
{
public DbSet<Blog> Blogs { get; set; } = null!;
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>().Property(b => b.SearchVector).HasColumnType("tsvector");
modelBuilder.Entity<Blog>().Property(b => b.SearchVector).Metadata.SetTsVectorConfig("english");
modelBuilder.Entity<Blog>().Property(b => b.SearchVector).Metadata
.SetTsVectorProperties(["Title"]); // new string[] { "Title" } - ok
}
}
public class Blog
{
public int Id { get; set; }
public string Title { get; set; } = null!;
public NpgsqlTsVector SearchVector { get; set; }
}
This is a narrow edge case, but the fix should be a one-liner. I'm happy to submit a PR with the fix if that's useful.
The below is an AI-generated analysis and may contain inaccuracies.
Confirmed bug; PostgreSQL-provider-specific. Both SetTsVectorProperties overloads accept and store IReadOnlyList<string>, but the getter casts the annotation to string[]. A collection expression targeting IReadOnlyList<string>, a List<string>, or a ReadOnlyCollection<string> therefore causes InvalidCastException. Passing an actual string[] works.
Version checks. The standalone console reproduction fails on provider 5.0.0, 8.0.11, 9.0.4, 10.0.0, 10.0.3, and current main (e9df639b). No previous working version was identified for non-array input; this is longstanding, not a recent regression. The same array input succeeds on 10.0.3.
This fails while generating a create script, without connecting to PostgreSQL. As a provider control, SQLite 10.0.3 generates its script successfully with the same metadata and an explicit NpgsqlTsVector-to-text conversion, which SQLite needs to map that PostgreSQL-specific CLR type. SQLite ignores the PostgreSQL annotations; the failing annotation getter belongs to Npgsql, not EF Core itself.
Fix verification.#3984 changes the getter cast to IReadOnlyList<string>?, matching the public contract. I applied the exact PR patch and confirmed:
Its regression test fails with the original getter and passes with the fix; all 23 tests in NpgsqlMetadataExtensionsTest pass with the patch.
The standalone application successfully generates the expected tsvector GENERATED ALWAYS AS (to_tsvector('english', "Title")) STORED column with collection-expression, array, List<string>, and read-only-collection inputs through both mutable and convention setters.
Duplicate search. No matching issue found in dotnet/efcore. Related provider issues #3515 and #3246 concern column naming/resolution, not this collection cast, and are not duplicates.
minimal repro
Use a .NET 10 console project with Npgsql.EntityFrameworkCore.PostgreSQL 10.0.3. No running database is required.
Activity
roji commentedon Oct 17, 2026
AI Triage
The below is an AI-generated analysis and may contain inaccuracies.
Confirmed bug; PostgreSQL-provider-specific. Both
SetTsVectorPropertiesoverloads accept and storeIReadOnlyList<string>, but the getter casts the annotation tostring[]. A collection expression targetingIReadOnlyList<string>, aList<string>, or aReadOnlyCollection<string>therefore causesInvalidCastException. Passing an actualstring[]works.Version checks. The standalone console reproduction fails on provider 5.0.0, 8.0.11, 9.0.4, 10.0.0, 10.0.3, and current main (
e9df639b). No previous working version was identified for non-array input; this is longstanding, not a recent regression. The same array input succeeds on 10.0.3.This fails while generating a create script, without connecting to PostgreSQL. As a provider control, SQLite 10.0.3 generates its script successfully with the same metadata and an explicit
NpgsqlTsVector-to-text conversion, which SQLite needs to map that PostgreSQL-specific CLR type. SQLite ignores the PostgreSQL annotations; the failing annotation getter belongs to Npgsql, not EF Core itself.Fix verification. #3984 changes the getter cast to
IReadOnlyList<string>?, matching the public contract. I applied the exact PR patch and confirmed:NpgsqlMetadataExtensionsTestpass with the patch.tsvector GENERATED ALWAYS AS (to_tsvector('english', "Title")) STOREDcolumn with collection-expression, array,List<string>, and read-only-collection inputs through both mutable and convention setters.No additional SQL-generation change is needed: the relational annotation provider already converts the property list to an array of column names.
Duplicate search. No matching issue found in
dotnet/efcore. Related provider issues #3515 and #3246 concern column naming/resolution, not this collection cast, and are not duplicates.minimal repro
Use a .NET 10 console project with
Npgsql.EntityFrameworkCore.PostgreSQL10.0.3. No running database is required.Expected: a create script is generated. Actual on 10.0.3:
Replacing
["Title"]withnew string[] { "Title" }avoids the failure. The original collection expression succeeds with #3984.[10.0] Fix tsvector metadata cast for IReadOnlyList (#3986)
[8.0] Fix tsvector metadata cast for IReadOnlyList (#3985)