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