【发布时间】:2023-04-11 06:00:01
【问题描述】:
我尝试使用实体框架核心 3.0 (Microsoft.EntityFrameworkCore.Cosmos 3.0.0) 操作 CosmosDB (SQL)。
除了我尝试使用Contains,StartWith,…
例如:
var query = _context.Challenges.Where(c => c.Name.Contains( "string"));
EF 应该将其转换为以下 SQL(在 CosmosDB – 查询资源管理器上完美运行)
SELECT * FROM c WHERE CONTAINS(c.Name, "string")
但我收到以下错误消息:
The LINQ expression 'Where<Challenge>(\n source: DbSet<Challenge>, \n predicate: (c) => c.Name.Contains(\"string\"))' could not be translated.
Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync().
See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
当然,我不想像下面这样编写代码,那样会在客户端执行整个包含,只是为了做一个简单的 LIKE……
List<Challenge> entities = _context.Challenges.AsEnumerable().Where(c => c.Name.Contains( "string")).ToList();
有人想在服务器端评估“包含”吗?
注意:我尝试使用 UseSqlServer 而不是 UseCosmos 完全相同的代码(并通过添加所需的 [Key] 注释并创建 SQL 服务器),它就像一个魅力......所以它绝对是 CosmosDB vs EF问题。
【问题讨论】:
-
查看可能的重复评论。那里有几个链接可以帮助你。
-
嗨 HazardousGlitch,感谢您的回复,但这不是重复的(只是想通过链接)。我认为这是围绕 CosmosDB EF 转换为 SQL 的特定问题。我只有一个隐式 Select 和一个 Contains 它应该可以与 EF core 3.0 一起正常工作。我没有特定的客户端代码...
标签: azure-cosmosdb entity-framework-core-3.0