使用 Cosmos DB 配置 EF Core 以存储字符串的 ICollection

Ste*_*han 7 c# entity-framework-core azure-cosmosdb azure-cosmosdb-sqlapi

实体框架有一些关于嵌入实体的很好的文档,但我不知道如何嵌入一个简单的字符串数组IEnumerable<string>

样板课

public class Post {
  public string Id {get;set;}
  public string Content {get;set;}
  public IEnumerable<string> Tags {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

这应该在 cosmos 中保存为:

{
  "id": "xxx",
  "content": "long content here",
  "tags": ["tag1", "tag2"],
  ...
}
Run Code Online (Sandbox Code Playgroud)

我知道我必须在OnModelCreating(ModelBuilder modelBuilder)上下文中配置一些东西。但我无法正确设置它。

我尝试过以下选项(以及其他几种ToJsonProperty方法):

  • modelBuilder.Entity<Post>().OwnsMany(p => p.Tags);
  • modelBuilder.Entity<Post>().OwnsMany<string>(p => p.Tags);

最终我希望能够根据这些标签进行查询,非常感谢任何帮助或正确方向的指示!

我也找到了这个答案,但是将数组转换为逗号分隔的字符串会达不到目的(因为这不允许我们查询这些帖子)。

其他人在 Microsoft 论坛上问了大致相同的问题,其中一位 Microsoft 员工表示,在纯 CosmosDB 中,可以在 cosmos 中嵌入字符串数组。

Ste*_*han 3

我在 Cosmos 的 EF 核心提供商处发现了一个跟踪此问题的github 问题。

所以官方的回答是,目前不支持。至少在上述问题解决之前。

更新,2021 年 7 月,EfCore 6.0.0 似乎现在支持此功能