Ali Kleit
Ali Kleit

Reputation: 3649

EF Core 5: FreeText Failed to convert parameter value from a String to a Byte[] on a Full-Text indexed Byte[] column

I have the following entity with a property Content of type byte[]:

public class Post
{
  [Column(TypeName = "varbinary(max)")]
  [Required]
  public byte[] Content { get; set; }
  
  public string ContentType { get; set; }
}

and the content type in model builder:

modelBuilder.Entity<Post>()
    .Property(p => p.ContentType)
    .HasComputedColumnSql("'.html'");

The Migration:

migrationBuilder.Sql("CREATE FULLTEXT CATALOG ft_catalog AS DEFAULT", true);
migrationBuilder.Sql(@"CREATE FULLTEXT INDEX ON dbo.Posts([Content] TYPE COLUMN ContentType) KEY INDEX PK_Posts", true);

EF.Functions.FreeText takes a string propertyReference and a string freeText.

Here, I passed the Column "Content" as a string I got The expression passed to the 'propertyReference' parameter of the 'FreeText' method is not a valid reference to a property. The expression should represent a reference to a full-text indexed property on the object referenced in the from clause.

IQueryable<DataSets.Post> query = db.Posts;

query = query.Where(k => EF.Functions.FreeText("Content", model.keyword)); 

Then, I looked around and found EF.Property and used it on propertyReference param, Then I got Failed to convert parameter value from a String to a Byte[].

query = query.Where(k => EF.Functions.FreeText(EF.Property<string>(k, "Content"), model.keyword));

The following SQL query works perfectly against the database:

select ID,Title,Description from Posts where FREETEXT(Content,'wolf')

Upvotes: 1

Views: 722

Answers (0)

Related Questions