Question

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.

1
1
4/10/2019 6:36:44 AM

Accepted Answer

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.

1
4/10/2019 6:35:33 AM

Popular Answer

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.

See https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.sqlserverdbfunctionsextensions.freetext?view=efcore-2.1

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.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow