【问题标题】:FreeText in EF2.1: The 'FreeText' method is not supported because the query has switched to client-evaluation.EF2.1 中的 FreeText:不支持“FreeText”方法,因为查询已切换到客户端评估。
【发布时间】:2019-10-21 15:15:22
【问题描述】:

我正在尝试做简单的全文搜索:

var dbquery = _dbContext.Contents
   .Where(c => EF.Functions.FreeText("Content", "a"));

var result = dbQuery.ToList();

但我得到了:

InvalidOperationException:不支持“FreeText”方法 因为查询已切换到客户端评估。检查日志 以确定哪些查询表达式正在触发客户端评估。

也许需要注意的是,我使用的是继承:

public class MyDbContext : DbContext
{
    public DbSet<ForumThread> ForumThreads { get; set; }
    public DbSet<ForumPost> ForumPosts { get; set; }

    public DbSet<ContentBase> Contents { get; set; }
 }

public abstract class ContentBase
{
    public Guid Id { get; set; }
    public string Content { get; set; }
}

public class GenericContent : ContentBase
{        
    public virtual string Title { get; set; }
    public virtual ICollection<Tag> Tags { get; set; } = new List<Tag>();
}

public class ForumThread : GenericContent {}

public class ForumPost : ContentBase
{
    public ForumThread Thread { get; set; }
}

【问题讨论】:

  • EF.Functions.FreeText("Content", "a") 不使用 c 中的任何内容。你的意思是EF.Functions.FreeText(c.Content, "a")
  • @hvd:你是对的,但你是怎么知道的? Documentation 说应该是属性名
  • 嗯。糟糕的文档。这对我来说没有意义,我认为它应该是c.Content,并通过快速的 Google 搜索进行了仔细检查。我建议在他们的 GitHub 问题页面上打开一个问题,但看起来参数已经重命名为 propertyReference,这至少好一点。我认为您可以根据当前文档发布解释为什么您认为它应该是"Content",以及为什么结果不应该是,如果您愿意,可以作为答案。
  • 无论如何,我会接受它作为答案,如果你写一个

标签: c# entity-framework-core asp.net-core-2.1


【解决方案1】:

另一种方式:

var dbquery = _dbContext.Contents
    .Where(c => EF.Functions.FreeText(EF.Property<string>(c, "Content"), "a"));

【讨论】:

  • 您能解释一下您的解决方案吗?
  • 在我的情况下解决了与EF.Functions.Contains 相同的问题。不知道为什么或如何,因为文档不存在。
猜你喜欢
  • 1970-01-01
  • 2011-05-26
  • 1970-01-01
  • 2020-07-08
  • 2019-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多