【问题标题】:EF Core 2.1 filter by owned typeEF Core 2.1 按自有类型过滤
【发布时间】:2019-02-11 22:24:06
【问题描述】:

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

我有以下值对象

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;}
}

当我用say查询数据库时

_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表中只有几条记录,但是记录多了会影响性能吗?是否有解决方法。

【问题讨论】:

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


    【解决方案1】:

    您期望 EF Core 将本地客户端代码(不是表达式)转换为 SQL 代码,但它不会这样做,这就是为什么它告诉您它将在本地进行评估。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多