我使用 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"
我错过了什么?有人知道出了什么问题吗?
先感谢您。