I'm working on a project which uses Entity Framework Cor 2.1 and Aspnet Core Web API.
I have the following ValueObject
public class Email
{
private string _value;
private Email(string value) => _value = value;
public static Email Create(string email)
{
//... code hidden for clarity
return new Email(email);
}
// I have overridden equality operators to check equality by the _value property
// and also ToString to return _value
}
I have this value object configured as an owned type in Person entity.
public class Person
{
//... code hidden for clarity
public virtual Email Email {get; private set;}
}
When I query the database with say
_context.People.Where(person => person.Email == Email.Create("example@example.com");
I get the following warning on the console.
The LINQ expression 'where ([person.Email] == __email_0)' could not be translated and will be evaluated locally.
I only have a few records in the Person table, but will it affect the performance when it has more records? and is there a workaround for this.
You're expecting EF Core to convert local client code (not expressions) to SQL code, which it doesn't do and is why it's telling you that it will be evaluated locally.
I'd suggest implementing a value converter where the ConvertToProviderExpression
and ConvertFromProviderExpression
expressions can handle your custom logic storing to and reading from the DB.