Skip to content

SetTsVectorProperties throws InvalidCastException when passed a non-array IReadOnlyList<string> #3892

Closed

Description

@hostage2222

NpgsqlPropertyExtensions.SetTsVectorProperties accepts IReadOnlyList<string>?, but the corresponding getter, GetTsVectorProperties, reads the annotation back with a hard cast to string[]?:

src/EFCore.PG/Extensions/MetadataExtensions/NpgsqlPropertyExtensions.cs:1098

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:

modelBuilder.Entity<Blog>().Property(b => b.SearchVector).Metadata.SetTsVectorProperties(["Title"])

Repro
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.

Activity

  1. added theissue type on Oct 17, 2026
  2. roji commented on Oct 17, 2026

    @roji
    Member

    AI Triage

    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.

    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.PostgreSQL 10.0.3. No running database is required.

    using Microsoft.EntityFrameworkCore;
    using Microsoft.Extensions.Logging;
    using NpgsqlTypes;
    
    using var context = new BlogContext();
    context.Database.GenerateCreateScript();
    
    public class BlogContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; } = null!;
    
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder
                .UseNpgsql("Host=localhost;Database=repro")
                .LogTo(Console.WriteLine, LogLevel.Information)
                .EnableSensitiveDataLogging();
    
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            var property = modelBuilder.Entity<Blog>()
                .Property(b => b.SearchVector)
                .HasColumnType("tsvector")
                .Metadata;
            property.SetTsVectorConfig("english");
            property.SetTsVectorProperties(["Title"]);
        }
    }
    
    public class Blog
    {
        public int Id { get; set; }
        public string Title { get; set; } = null!;
        public NpgsqlTsVector SearchVector { get; set; } = null!;
    }

    Expected: a create script is generated. Actual on 10.0.3:

    System.InvalidCastException: Unable to cast object of type
    '<>z__ReadOnlySingleElementList`1[System.String]' to type 'System.String[]'.
    

    Replacing ["Title"] with new string[] { "Title" } avoids the failure. The original collection expression succeeds with #3984.

  3. added this to the 10.0.4 milestone on Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions