2

我使用 CosmosDB (Sql Core) + EFCore (3.1.10) + CosmosDb 提供程序 (3.1.10)。

我的实体有属性 ID(以大写字母开头),没有关闭歧视。

因此,根据这篇文章和其他文章,我希望 id 将作为鉴别器和 Id(“EntityType|IdValue”)的组合填充。但是,在我的情况下,id 仅填充了鉴别器值我的代码:实体:

public class TestEntity3
{
    public string Id { get; set; }
    public string Prop3 { get; set; }
}

数据库上下文:

class TestEntity3DbContext : DbContext
{
    public DbSet<TestEntity3> TestEntities3 { get; protected set; } = null!;

    public TestEntity3DbContext(DbContextOptions<TestEntity3DbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        builder.HasDefaultContainer("UnifiedStorage");
        builder.Entity<TestEntity3>(o =>
        {
            o.HasKey(o => o.Id);
            o.HasPartitionKey(o => o.Id);
        });
    }
}

要创建记录,我使用以下代码:

        _testEntityContext.TestEntities3.Add(new TestEntity3()
        {
            Id = Guid.NewGuid().ToString(),
            Prop3 = "Hello"
        });
        await _testEntityContext.SaveChangesAsync();

结果,我看到了这条记录(删除了几个字段):

{
"Id": "fe2b7107-f9d0-4c7d-a594-f6bccaab36cd",
"Discriminator": "TestEntity3",
"Prop3": "Hello",
"id": "TestEntity3",
"_ts": 1605731991

}

所以,有 id = "TestEntity3",但它应该是 "TestEntity3|fe2b7107-f9d0-4c7d-a594-f6bccaab36cd"

我错过了什么?有人知道出了什么问题吗?

先感谢您。

4

1 回答 1

1

我也看到了这一点,并相信这是设计使然。

当您对 partition key 和 primary key 使用相同的属性时,就会发生这种情况。这个想法是id在每个分区中应该是唯一的。在这种情况下,您是说每个分区键都是唯一的(即每个分区中只有一个实体),因此id除了跟踪鉴别器之外,没有理由将其用于任何事情。

于 2021-04-12T08:13:57.930 回答