c# - 使用 OwnsOne() 时,“FromSqlRaw 或 FromSqlInterpolated 是使用不可组合的 SQL 调用的,并在其上组合了一个查询”

标签 c# entity-framework-core

我有几个用于搜索数据库的存储过程。每个返回几个字段与找到/返回的数据量有关。模型看起来像这样:

// simplified
public abstract class SearchResult {
  public int RowCount { get; set; }
  public int FilteredRowCount { get; set; }
  // other properties
}

public class FooSearch {
  public string Id { get; set; }
  public string Foo { get; set; }
  public SearchResult Result { get; set; }
}

然后在 DBContext 中,我使用 OwnsOne() 链接类,根据 this MS guide

public DbSet<FooSearch> FooSearch { get; set; }

// OnModelCreating
builder.Entity<FooSearch>().OwnsOne(t => t.Result);

最后我进行了 SP 调用:

var searchResult = db.FooSearch.FromSqlRaw("EXECUTE [Search].[Foo] {0}, {1}", foo, bar).ToList();

但是这最后一步给我以下错误:

System.InvalidOperationException: 'FromSqlRaw or FromSqlInterpolated was called with non-composable SQL and with a query composing over it. Consider calling AsEnumerable after the FromSqlRaw or FromSqlInterpolated method to perform the composition on the client side.'

ToList() 更改为 AsEnumerable() 对结果没有影响。

如果我从 FooSearch 类中删除 OwnsOne()Result 属性,那么 SP 会工作并且我会得到结果。此问题的原因是什么,我该如何解决?

最佳答案

我遇到了同样的问题并在阅读问题 #18232 后修复了它.

您需要在FromSqlRaw 之后添加.IgnoreQueryFilters()。像那样:

var searchResult = db.FooSearch
.FromSqlRaw("EXECUTE [Search].[Foo] {0}, {1}", foo, bar)
.IgnoreQueryFilters()
.ToList();

关于c# - 使用 OwnsOne() 时,“FromSqlRaw 或 FromSqlInterpolated 是使用不可组合的 SQL 调用的,并在其上组合了一个查询”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61076338/

相关文章:

c# - 我应该为客户端连接选择哪种异常处理

c# - EF2.1 中的自由文本 : The 'FreeText' method is not supported because the query has switched to client-evaluation.

c# - 将 Serilog.Sinks.MsSqlServer 与 Entity Framework Core 结合使用

c# - 迁移中未应用的最大长度

c# - Entity Framework 7 与 .Net 中的现有数据库 MVC 6

c# - 使用 EF Core 重用 MVVM 模式中的 View

c# - 在 C# 中管理 COM 对象

c# - WPF 绑定(bind)设计时 "Property Expected"错误

c# - 如何模拟登录用户并访问 unc 文件夹?

c# - Visual C# - 缺少部分修饰符