EF Core 2.1拥有类型的过滤器

时间:2018-09-06 15:38:47

标签: entity-framework-core domain-driven-design ef-core-2.1 owned-types

我正在研究一个使用Entity Framework Cor 2.1和Aspnet Core Web API的项目。

我有以下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
}

我将此值对象配置为Person实体中的所有类型。

public class Person
{
    //... code hidden for clarity
    public virtual Email Email {get; private set;}
}

当我用

查询数据库时
_context.People.Where(person => person.Email == Email.Create("example@example.com");

我在控制台上收到以下警告。

 The LINQ expression 'where ([person.Email] == __email_0)' could not be translated and will be evaluated locally.

Person表中只有几条记录,但是当它有更多记录时会影响性能吗?对此有解决方法。

1 个答案:

答案 0 :(得分:1)

您期望EF Core将本地客户端代码(而不是表达式)转换为SQL代码,这是不起作用的,这就是为什么它告诉您将在本地对其进行评估。

我建议实现一个value converter,其中ConvertToProviderExpressionConvertFromProviderExpression表达式可以处理您自定义逻辑存储到数据库和从数据库读取的逻辑。