Reputation: 937
EF Core 2.1 has added support for FREETEXT
, as also discussed in How to use FreeText in EF core 2.1. I have a different problem, however, using EF Core 2.2: Does EF Core's FREETEXT
support child entities?
public class Customer
{
public Name Name { get; set; }
public List<Address> Addresses { get; set; }
}
Name is an owned entity (value object), which works perfectly for searching:
public class Name
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
Address is a child entity:
public class Address
{
public string Street { get; set; }
public string Number { get; set; }
}
This search works fine:
query.Where(c => EF.Functions.Freetext(c.Name.FirstName, searchTerm) || EF.Functions.Freetext(c.Name.LastName, searchTerm)
This search does not, as the final term cannot be translated to SQL:
query.Where(c => EF.Functions.Freetext(c.Name.FirstName, searchTerm) || EF.Functions.Freetext(c.Name.LastName, searchTerm) || EF.Functions.Freetext(c.Addresses.First().Street, searchTerm)
Is there any way around this, or will I need to use a SQL function? I've tried using a Select()
statement, but that could also not be fully translated to SQL.
Upvotes: 1
Views: 1569
Reputation: 937
Found it! Apparently, EF.Functions.FreeText(c.Addresses.First().Street, searchTerm)
cannot be evaluated client-side. However, this can:
EF.Functions.FreeText(c.Addresses.Any(a => EF.Functions.FreeText(a.Street, searchTerm))
So make sure EF.Functions.FreeText()
receives a simple string
as its first property, and use any other LINQ for selecting the First()
, 'Last()', Any()
and All()
of child entities.
Upvotes: 1
Reputation: 39
The Docs for the FREETEXT method from EF Core 2.1 indicate that client-evaluation is not allowed. There is no Docs for EF Core 2.2 yet, but I assume it's not changed.
This DbFunction method has no in-memory implementation and will throw if the query switches to client-evaluation.
This can happen if the query contains one or more expressions that could not be translated to the store.
Otherwise, you might consider adding a property on Customer, on which you can query directly, if it's really important for you to use FREETEXT. For example
public class Customer
{
public Name Name { get; set; }
public List<Address> Address { get; set; }
public string DefaultStreet { get; set; }
}
I assume the addresses are on a list, based on your query.
Upvotes: 0